Created
August 14, 2016 19:39
-
-
Save MHMDhub/9436c34567327b69178c23fc742cde55 to your computer and use it in GitHub Desktop.
Merge two equal-length lists into a dictionary, mapping one-to-many
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
Lst1 = ['11', '13', '11', '12', '11', '13', '12', '12', '12', '13', '11'] | |
Lst2 = ['1/41', '1/34', '1/37', '1/47', '1/41', '1/33', '1/46', '1/45', 'p4', 'p5', 'p6'] | |
Dict1 = {'11': ['1/41', '1/37', '1/141', 'p6'], | |
'12': ['1/47', '1/33', '1/46', 'p4'], | |
'13': ['1/34', '1/33', 'p5']} | |
Dict1 = {} | |
for key, val in zip(Lst1, Lst2): | |
Dict1.setdefault(key, []).append(val) | |
Dict1 | |
{'11': ['1/41', '1/37', '1/41', 'p6'], '13': ['1/34', '1/33', 'p5'], '12': ['1/47', '1/46', '1/45', 'p4']} | |
from pprint import pprint | |
pprint(Dict1) | |
{'11': ['1/41', '1/37', '1/41', 'p6'], | |
'12': ['1/47', '1/46', '1/45', 'p4'], | |
'13': ['1/34', '1/33', 'p5']} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment