Turning a list of dictionaries into a single larger dictionary
Question:
Maybe this is a simple question, but I’ve been scratching my head on it.
How do I turn a list of several dictionaries into a single dictionary containing all values?
This is what I mean:
input_list = [
{'fruit': 'banana',
'color1': ''},
{'vegetable': 'tomato',
'color2': ''},
{'dessert': 'ice cream',
'taste': ''}
]
desired_output = {
'fruit': 'banana',
'color1': '',
'vegetable': 'tomato',
'color2': '',
'dessert': 'ice cream',
'taste': ''
}
Any help would be appreciated, thank you in advance.
Answers:
Introduction
You can merge dict
with the |
bitwise operator from version 3.9
. chaining them, with functools.reduce
with operator.or_
:
Rough explanation of functools.reduce
functools.reduce(function, iterable[, initializer])
Apply function of two arguments cumulatively to the items of iterable, from left to right, so as to reduce the iterable to a single value
Here are some examples for functools.reduce:
>>> #calculates ((((1+2)+3)+4)+5) which is the sum
>>> reduce(lambda x, y: x+y, [1, 2, 3, 4, 5])
15
>>> # factorial of 5 using reduce
>>> n = 4
>>> reduce(lambda x, y: x*y, range(1,n+1))
24
Answer
Using functools.reduce
and operator._or
here is the code that I come up with:
import operator
from functools import reduce
result = reduce(operator.or_,list_of_dicts)
pprint.pprint(result)
Output
{'fruit': 'banana',
'color1': '',
'vegetable': 'tomato',
'color2': '',
'dessert': 'ice cream',
'taste': ''}
Use this:
output = {key:value for element in input_list for key, value in element.items()}
Output:
{'fruit': 'banana',
'color1': '',
'vegetable': 'tomato',
'color2': '',
'dessert': 'ice cream',
'taste': ''}
You could just loop through the list and use update() method to merge the dictionaries that is all needed to do:
desired_output = {}
for d in input_list:
desired_output.update(d)
Maybe this is a simple question, but I’ve been scratching my head on it.
How do I turn a list of several dictionaries into a single dictionary containing all values?
This is what I mean:
input_list = [
{'fruit': 'banana',
'color1': ''},
{'vegetable': 'tomato',
'color2': ''},
{'dessert': 'ice cream',
'taste': ''}
]
desired_output = {
'fruit': 'banana',
'color1': '',
'vegetable': 'tomato',
'color2': '',
'dessert': 'ice cream',
'taste': ''
}
Any help would be appreciated, thank you in advance.
Introduction
You can merge dict
with the |
bitwise operator from version 3.9
. chaining them, with functools.reduce
with operator.or_
:
Rough explanation of functools.reduce
functools.reduce(function, iterable[, initializer])
Apply function of two arguments cumulatively to the items of iterable, from left to right, so as to reduce the iterable to a single value
Here are some examples for functools.reduce:
>>> #calculates ((((1+2)+3)+4)+5) which is the sum
>>> reduce(lambda x, y: x+y, [1, 2, 3, 4, 5])
15
>>> # factorial of 5 using reduce
>>> n = 4
>>> reduce(lambda x, y: x*y, range(1,n+1))
24
Answer
Using functools.reduce
and operator._or
here is the code that I come up with:
import operator
from functools import reduce
result = reduce(operator.or_,list_of_dicts)
pprint.pprint(result)
Output
{'fruit': 'banana',
'color1': '',
'vegetable': 'tomato',
'color2': '',
'dessert': 'ice cream',
'taste': ''}
Use this:
output = {key:value for element in input_list for key, value in element.items()}
Output:
{'fruit': 'banana',
'color1': '',
'vegetable': 'tomato',
'color2': '',
'dessert': 'ice cream',
'taste': ''}
You could just loop through the list and use update() method to merge the dictionaries that is all needed to do:
desired_output = {}
for d in input_list:
desired_output.update(d)