Python merge list of dictionaries
How can I turn a list of dicts like Show Answers here will overwrite keys that match between two of the input dicts, because a dict cannot have duplicate keys. If you want to collect multiple values from matching keys, see How to merge dicts, collecting values from matching keys?.
asked Aug 16, 2010 at 15:55
5 This works for dictionaries of any length:
As a comprehension:
wim 312k95 gold badges570 silver badges709 bronze badges answered Aug 16, 2010 at 16:56 0 In case of Python 3.3+, there is a
Also see:
answered Jan 13, 2016 at 5:50
alecxealecxe 448k114 gold badges1042 silver badges1167 bronze badges 1 Little improvement for @dietbuddha answer with dictionary unpacking from PEP 448, for me, it`s more readable this way, also, it is faster as well:
But keep in mind, this works only with Python 3.5+ versions. answered Feb 9, 2020 at 17:09
1 This is similar to @delnan but offers the option to modify the k/v (key/value) items and I believe is more readable:
for instance, replace k/v elems as follows:
unpacks the k,v tuple from the dictionary .items() generator after pulling the dict object out of the list answered May 4, 2018 at 16:29
SchaltonSchalton 2,5752 gold badges30 silver badges41 bronze badges For flat dictionaries you can do this:
SiHa 7,12112 gold badges32 silver badges42 bronze badges answered Apr 16, 2013 at 22:36
dietbuddhadietbuddha 8,26829 silver badges33 bronze badges
Note: the order of 'b' and 'c' doesn't match your output because dicts are unordered if the dicts can have more than one key/value
answered Aug 16, 2010 at 16:38
John La RooyJohn La Rooy 286k51 gold badges358 silver badges498 bronze badges 3 You can use join function from funcy library:
answered Jun 4, 2014 at 21:05
SuorSuor 2,6631 gold badge20 silver badges28 bronze badges 1 If you don't need the singleton dicts anymore:
answered Oct 2, 2021 at 12:41
no commentno comment 6,1854 gold badges10 silver badges30 bronze badges
This is asymmetrical because you need to choose what to do with duplicate keys; in this case, EDIT: Ah, sorry, didn't see that. It is possible to do this in a single expression:
No credit to me for this last! However, I'd argue that
it might be more Pythonic (explicit > implicit, flat > nested ) to do this with a simple answered Aug 16, 2010 at 15:58
KatrielKatriel 116k19 gold badges133 silver badges165 bronze badges 1
Note I added a second key/value pair to the last dictionary to show it works with multiple entries. Also keys from dicts later in the list will overwrite the same key from an earlier dict. answered Aug 16, 2010 at 16:59
Dave KirbyDave Kirby 25k5 gold badges64 silver badges82 bronze badges this way worked for me:
printing object:
thanks Axes answered Sep 26 at 13:14
How do I concatenate a list of dictionaries in Python?Below are the eight standard methods by which you can merge two dictionaries in python.. 1) Using update() method.. 2) Using merge(|) operator.. 3) Using ** operator.. 4) Unpacking the second dictionary.. 5) Using collection.ChainMap() method.. 6) Using itertools. ... . 7) Using dictionary comprehension.. 8) Add values of common keys.. Can you merge dictionaries Python?Python 3.9 has introduced the merge operator (|) in the dict class. Using the merge operator, we can combine dictionaries in a single line of code. We can also merge the dictionaries in-place by using the update operator (|=).
How do I merge a list of Dicts into a single dict?To merge multiple dictionaries, the most Pythonic way is to use dictionary comprehension {k:v for x in l for k,v in x. items()} to first iterate over all dictionaries in the list l and then iterate over all (key, value) pairs in each dictionary.
How do you merge lists in Python?One simple and popular way to merge(join) two lists in Python is using the in-built append() method of python. The append() method in python adds a single item to the existing list. It doesn't return a new list of items. Instead, it modifies the original list by adding the item to the end of the list.
|