Created
March 22, 2020 16:14
-
-
Save aliaramli/7262c8bebf6daa4366cfa04e2c67de35 to your computer and use it in GitHub Desktop.
Python data struct dict group by 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
from collections import defaultdict | |
def group_by_owners(files): | |
# Grouping dictionary keys by value | |
res = defaultdict(list) | |
for key, val in files.items(): | |
res[val].append(key) | |
return res | |
if __name__ == "__main__": | |
files = { | |
'Input.txt': 'Randy', | |
'Code.py': 'Stan', | |
'Output.txt': 'Randy' | |
} | |
print(group_by_owners(files)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment