dictionary - Mapping (key, value) (key, value) to (key, (value1, value2)) in Python -
I have a list of Tupe's forms (user, page):
Tulips = [(Tim, Papufu), (Jane, Pager), (Tim, PageBaz), ...] I should loop in this list and a new list or vowel pages per page :
newlist = [(Tim, [pagefu, pagebuzz]), (Jane, [pager]), ...] It is coming that this page is not in user order - as I loop, let me see this New new list will check the exact user already exists or not, and if you update it, the list.
Can someone help me to get through my mental barrier?
@ AVIVinee is fast, but here defaultdict is the solution: Importing from Import default default Tippers = [('Tim', 'Papufu'), ('Jane', 'Pager'), ('Tim', 'PageBaz')] Results = defaultdict (List) key, in tuples value: result [key] .append (value) result # out [8]: defaultdict (& lt; type 'list' & gt ;, '' Tim ': [' page photo ## [9]: {'Jane': ['Pager'], 'Tim': ['PageFoo', 'O', 'Pbz'], 'Jane': ['Pager']} PageBaz ']} Results [edit] Please] [out] [10]: [('T The '[' Pejfu 'Pejbajh']), (] 'Jane', [ 'pager')]
Comments
Post a Comment