Created
August 30, 2017 13:09
-
-
Save Kwpolska/db6333035f960b0128c7748d18d38cc4 to your computer and use it in GitHub Desktop.
Split dict of lists into dict of values
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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