Skip to content Skip to sidebar Skip to footer

Convert List Of Single Key Dictionaries Into A Single Dictionary

I have a list of single-key dictionaries. For example: lst = [ {'1': 'A'}, {'2': 'B'}, {'3': 'C'} ] I'd like to simply convert this into a normal dictionary: dictionar

Solution 1:

You can use reduce:

reduce(lambda r, d: r.update(d) or r, lst, {})

Demo:

>>> lst = [
...     {'1': 'A'},
...     {'2': 'B'},
...     {'3': 'C'}
... ]
>>> reduce(lambda r, d: r.update(d) or r, lst, {})
{'1': 'A', '3': 'C', '2': 'B'}

or you could chain the items calls (Python 2):

from itertools import chain, imap
from operator import methodcaller

dict(chain.from_iterable(imap(methodcaller('iteritems'), lst)))

Python 3 version:

from itertools import chain
from operator import methodcaller

dict(chain.from_iterable(map(methodcaller('items'), lst)))

Demo:

>>> from itertools import chain, imap
>>> from operator import methodcaller
>>> >>> dict(chain.from_iterable(map(methodcaller('iteritems'), lst)))
{'1': 'A', '3': 'C', '2': 'B'}

Or use a dict comprehension:

{k: v for d in lst for k, v in d.iteritems()}

Demo:

>>> {k: v fordin lst fork, v in d.iteritems()}
{'1': 'A', '3': 'C', '2': 'B'}

Of the three, for the simple 3-dictionary input, the dict comprehension is fastest:

>>>import timeit>>>defd_reduce(lst):...    reduce(lambda r, d: r.update(d) or r, lst, {})...>>>defd_chain(lst):...dict(chain.from_iterable(imap(methodcaller('iteritems'), lst)))...>>>defd_comp(lst):...    {k: v for d in lst for k, v in d.iteritems()}...>>>timeit.timeit('f(lst)', 'from __main__ import lst, d_reduce as f')
2.4552760124206543
>>>timeit.timeit('f(lst)', 'from __main__ import lst, d_chain as f')
3.9764280319213867
>>>timeit.timeit('f(lst)', 'from __main__ import lst, d_comp as f')
1.8335261344909668

When you increase the number of items in the inputlist to 1000, then the chain method catches up:

>>>import string, random>>>lst = [{random.choice(string.printable): random.randrange(100)} for _ inrange(1000)]>>>timeit.timeit('f(lst)', 'from __main__ import lst, d_reduce as f', number=10000)
5.420135974884033
>>>timeit.timeit('f(lst)', 'from __main__ import lst, d_chain as f', number=10000)
3.464245080947876
>>>timeit.timeit('f(lst)', 'from __main__ import lst, d_comp as f', number=10000)
3.877490997314453

Increasing the input list further doesn't appear to matter from here on out; the chain() approach is a small percentage faster but never gains a clear advantage.

Solution 2:

answer = {}
for d in L:
    answer.update(d)

Output:

>>>L = [...    {'1': 'A'},...    {'2': 'B'},...    {'3': 'C'}...]>>>answer = {}>>>for d in L: answer.update(d)...>>>answer
{'2': 'B', '3': 'C', '1': 'A'}

OR

answer = {k:v for d in L for k,v in d.items()}

Post a Comment for "Convert List Of Single Key Dictionaries Into A Single Dictionary"