Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Merging Dictionaries Python

Hey guys i want to merge these 45 dictionaries into one. they all have the same keys.

d1 = {'Image': 'https://upload.wikimedia.org/wikipedia/commons/thumb/6/6e/Signal_Iduna_Park_before_the_match_%284th_july_2006%29.jpg/150px-Signal_Iduna_Park_before_the_match_%284th_july_2006%29.jpg', 'Stadium': 'Westfalenstadion', 'Capacity': '81,365', 'City': 'Dortmund', 'Population': '586,852', 'Postal Code': '44001-44388', 'State': 'North Rhine-Westphalia', 'Home Team': 'Borussia Dortmund', 'Opened Year': '1974', 'Notes': 'UEFA Category 4 stadium'}
d2 = {'Image': 'https://upload.wikimedia.org/wikipedia/commons/thumb/1/12/M%C3%BCnchen_-_Allianz-Arena_%28Luftbild%29.jpg/150px-M%C3%BCnchen_-_Allianz-Arena_%28Luftbild%29.jpg', 'Stadium': 'Allianz Arena', 'Capacity': '75,000', 'City': 'Munich', 'Population': '1,487,708', 'Postal Code': '80331–81929', 'State': 'Bavaria', 'Home Team': 'FC Bayern Munich', 'Opened Year': '2005', 'Notes': 'UEFA Category 4 stadium'}
d3 = {'Image': 'https://upload.wikimedia.org/wikipedia/commons/thumb/3/3c/Olympiastadionberlin2005.JPG/150px-Olympiastadionberlin2005.JPG', 'Stadium': 'Olympiastadion Berlin', 'Capacity': '74,475', 'City': 'Berlin', 'Population': '3,677,472', 'Postal Code': '', 'State': 'Berlin', 'Home Team': 'Hertha BSC', 'Opened Year': '1936', 'Notes': 'UEFA Category 4 stadium'}
d4 = {'Image': 'https://upload.wikimedia.org/wikipedia/commons/thumb/f/f0/Olympiastadion_Muenchen.jpg/150px-Olympiastadion_Muenchen.jpg', 'Stadium': 'Olympiastadion Munich', 'Capacity': '69,250', 'City': 'Munich', 'Population': '1,487,708', 'Postal Code': '80331–81929', 'State': 'Bavaria', 'Home Team': 'Various', 'Opened Year': '1972', 'Notes': ''}
d5 = {'Image': 'https://upload.wikimedia.org/wikipedia/commons/thumb/4/45/2010-06-03_Arena_AufSchalke_01.jpg/150px-2010-06-03_Arena_AufSchalke_01.jpg', 'Stadium': 'Veltins-Arena', 'Capacity': '62,271', 'City': 'Gelsenkirchen', 'Population': '', 'Postal Code': '45801-45899', 'State': 'North Rhine-Westphalia', 'Home Team': 'FC Schalke 04', 'Opened Year': '2001', 'Notes': 'UEFA Category 4 stadium'}
d6 = {'Image': 'https://upload.wikimedia.org/wikipedia/commons/thumb/5/52/Blick_vom_Rotenberg_Stadion.jpg/150px-Blick_vom_Rotenberg_Stadion.jpg', 'Stadium': 'Mercedes-Benz Arena', 'Capacity': '60,469', 'City': 'Stuttgart', 'Population': '626,275', 'Postal Code': '70173–70619', 'State': 'Baden-Württemberg', 'Home Team': 'VfB Stuttgart', 'Opened Year': '1933', 'Notes': 'UEFA Category 4 stadium'}

I tried it with defaultdict and update but it didnt work.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If I got you right, do the following:

from collections import defaultdict

merged = defaultdict(list)
for d in [d1, d2, d3...]:
    for key, value in d.items():
        d[key].append(value)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading