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.
>Solution :
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': ''}