Skip to content

Instantly share code, notes, and snippets.

@bmcorser
Created August 26, 2014 06:35
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 bmcorser/ec8f7b4d84d0e4cd9e20 to your computer and use it in GitHub Desktop.
Save bmcorser/ec8f7b4d84d0e4cd9e20 to your computer and use it in GitHub Desktop.
from collections import defaultdict
phonebook = [('betty', '555-2938'),
('betty', '342-2492'),
('bonnie', '452-2928'),
('patsy', '493-2928'),
('patsy', '943-2929'),
('patsy', '827-9162'),
('lucille', '205-2928'),
('wendy', '939-8282'),
('penny', '853-2492'),
('penny', '555-2111')]
d = defaultdict(list)
[d[a].append(b) for a, b in phonebook]
dict(d) == {
'lucille': ['205-2928'],
'penny': ['853-2492', '555-2111'],
'bonnie': ['452-2928'],
'patsy': ['493-2928', '943-2929', '827-9162'],
'betty': ['555-2938', '342-2492'],
'wendy': ['939-8282']
}
# True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment