Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

how to merge two dictionaries with same keys in python

from collections import defaultdict

d1 = {1: 2, 3: 4}
d2 = {1: 6, 3: 7}

dd = defaultdict(list)

for d in (d1, d2): # you can list as many input dicts as you want here
    for key, value in d.items():
        dd[key].append(value)

print(dd)
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #merge #dictionaries #keys #python
ADD COMMENT
Topic
Name
8+8 =