Skip to content

Instantly share code, notes, and snippets.

@Kwpolska
Created August 30, 2017 13:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kwpolska/db6333035f960b0128c7748d18d38cc4 to your computer and use it in GitHub Desktop.
Save Kwpolska/db6333035f960b0128c7748d18d38cc4 to your computer and use it in GitHub Desktop.
Split dict of lists into dict of values
def sep_unique(d):
newdict = {}
for key, val in d.items():
if len(val) > 1:
for n, i in enumerate(val, 1):
newdict[key if n == 1 else f"{key}{i}"] = i
else:
newdict[key] = val[0]
return newdict
print(sep_unique({'foo': [1, 2], 'bar': ['3']}))
# output: {'foo': 1, 'foo2': 2, 'bar': '3'}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment