Skip to content

Instantly share code, notes, and snippets.

@aliaramli
Created March 22, 2020 16:14
Show Gist options
  • Save aliaramli/7262c8bebf6daa4366cfa04e2c67de35 to your computer and use it in GitHub Desktop.
Save aliaramli/7262c8bebf6daa4366cfa04e2c67de35 to your computer and use it in GitHub Desktop.
Python data struct dict group by values
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