How can I turn a list of dicts like [{'a':1}, {'b':2}, {'c':1}, {'d':2}]
, into a single dict like {'a':1, 'b':2, 'c':1, 'd':2}
?
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:
>>> result = {}
>>> for d in L:
... result.update[d]
...
>>> result
{'a':1,'c':1,'b':2,'d':2}
As a comprehension:
# Python >= 2.7
{k: v for d in L for k, v in d.items[]}
# Python < 2.7
dict[pair for d in L for pair in d.items[]]
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 ChainMap
collection:
>>> from collections import ChainMap
>>> a = [{'a':1},{'b':2},{'c':1},{'d':2}]
>>> dict[ChainMap[*a]]
{'b': 2, 'c': 1, 'a': 1, 'd': 2}
Also see:
- What is the purpose of collections.ChainMap?
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:
from functools import reduce
result_dict = reduce[lambda a, b: {**a, **b}, list_of_dicts]
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:
new_dict = {k:v for list_item in list_of_dicts for [k,v] in list_item.items[]}
for instance, replace k/v elems as follows:
new_dict = {str[k].replace[" ","_"]:v for list_item in list_of_dicts for [k,v] in list_item.items[]}
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:
from functools import reduce
reduce[lambda a, b: dict[a, **b], list_of_dicts]
SiHa
7,12112 gold badges32 silver badges42 bronze badges
answered Apr 16, 2013 at 22:36
dietbuddhadietbuddha
8,26829 silver badges33 bronze badges
>>> L=[{'a': 1}, {'b': 2}, {'c': 1}, {'d': 2}]
>>> dict[i.items[][0] for i in L]
{'a': 1, 'c': 1, 'b': 2, 'd': 2}
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
>>> dict[j for i in L for j in i.items[]]
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:
from funcy import join
join[list_of_dicts]
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:
>>> L = [{'a':1}, {'b':2}, {'c':1}, {'d':2}]
>>> dict[map[dict.popitem, L]]
{'a': 1, 'b': 2, 'c': 1, 'd': 2}
answered Oct 2, 2021 at 12:41
no commentno comment
6,1854 gold badges10 silver badges30 bronze badges
dict1.update[ dict2 ]
This is asymmetrical because you need to choose what to do with duplicate keys; in this case, dict2
will overwrite dict1
. Exchange them for the other way.
EDIT: Ah, sorry, didn't see that.
It is possible to do this in a single expression:
>>> from itertools import chain
>>> dict[ chain[ *map[ dict.items, theDicts ] ] ]
{'a': 1, 'c': 1, 'b': 2, 'd': 2}
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 for
loop. YMMV.
answered Aug 16, 2010 at 15:58
KatrielKatriel
116k19 gold badges133 silver badges165 bronze badges
1
>>> dictlist = [{'a':1},{'b':2},{'c':1},{'d':2, 'e':3}]
>>> dict[kv for d in dictlist for kv in d.iteritems[]]
{'a': 1, 'c': 1, 'b': 2, 'e': 3, 'd': 2}
>>>
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:
object = [{'a':1}, {'b':2}, {'c':1}, {'d':2}]
object = {k: v for dct in object for k, v in dct.items[]}
printing object:
object = {'a':1,'b':2,'c':1,'d':2}
thanks Axes
answered Sep 26 at 13:14