Skip to content

Instantly share code, notes, and snippets.

@MHMDhub
Created August 14, 2016 19:39
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 MHMDhub/9436c34567327b69178c23fc742cde55 to your computer and use it in GitHub Desktop.
Save MHMDhub/9436c34567327b69178c23fc742cde55 to your computer and use it in GitHub Desktop.
Merge two equal-length lists into a dictionary, mapping one-to-many
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