Advertisement
here2share

# collect_dictionary.py

Jul 20th, 2022
980
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.34 KB | None | 0 0
  1. # collect_dictionary.py
  2.  
  3. from collections import defaultdict
  4.  
  5. def collect_dictionary(obj):
  6.   inv_obj = defaultdict(list)
  7.   for key, value in obj.items():
  8.     inv_obj[value].append(key)
  9.   return dict(inv_obj)
  10. ages = {
  11.   'Peter': 10,
  12.   'Isabel': 10,
  13.   'Anna': 9,
  14. }
  15. collect_dictionary(ages) # { 10: ['Peter', 'Isabel'], 9: ['Anna'] }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement