Python | Flatten given list of dictionaries
Given a list of the dictionaries, the task is to convert it into single dictionary i.e flattening a list of dictionaries.
Given below are a few methods to solve the given task.
Method #1: Using Naive Method
# Python code to demonstrate # to flatten list of dictionaries # Initialising dictionary ini_dict = [{'a':1}, {'b':2}, {'c':3}] # printing initial dictionary print ("initial dictionary", str(ini_dict)) # code to flatten lit of dictionary res = {} for d in ini_dict: res.update(d) # printing result print ("result", str(res)) |
chevron_right
filter_none
Output:
initial dictionary [{'a': 1}, {'b': 2}, {'c': 3}]
result {'b': 2, 'a': 1, 'c': 3}
Method #2: Using dict comprehension
# Python code to demonstrate # to flatten list of dictionaries # Initialising dictionary ini_dict = [{'a':1}, {'b':2}, {'c':3}] # printing initial dictionary print ("initial dictionary", str(ini_dict)) # code to flatten lit of dictionary res = {k: v for d in ini_dict for k, v in d.items()} # printing result print ("result", str(res)) |
chevron_right
filter_none
Output:
initial dictionary [{'a': 1}, {'b': 2}, {'c': 3}]
result {'a': 1, 'c': 3, 'b': 2}
Method #3: Using reduce
# Python code to demonstrate # to flatten list of dictionaries from functools import reduce # Initialising dictionary ini_dict = [{'a':1}, {'b':2}, {'c':3}] # printing initial dictionary print ("initial dictionary", str(ini_dict)) # code to flatten lit of dictionary res = reduce(lambda d, src: d.update(src) or d, ini_dict, {}) # printing result print ("result", str(res)) |
chevron_right
filter_none
Output:
initial dictionary [{'a': 1}, {'b': 2}, {'c': 3}]
result {'a': 1, 'c': 3, 'b': 2}
Method #4: Using collections.ChainMap
# Python code to demonstrate # to flatten list of # dictionaries from collections import ChainMap # Initialising dictionary ini_dict = [{'a':1}, {'b':2}, {'c':3}] # printing initial dictionary print ("initial dictionary", str(ini_dict)) # code to flatten lit of dictionary res = ChainMap(*ini_dict) # printing result print ("result", str(res)) |
chevron_right
filter_none
Output:
initial dictionary [{'a': 1}, {'b': 2}, {'c': 3}]
result ChainMap({'a': 1}, {'b': 2}, {'c': 3})
Recommended Posts:
- Python | Sum list of dictionaries with same key
- Python | Segregating key's value in list of dictionaries
- Python | Merging two list of dictionaries
- Python | Set Difference in list of dictionaries
- Python | Get values of particular key in list of dictionaries
- Python | Initialize list with empty dictionaries
- Python | Sort given list of dictionaries by date
- Python | Removing dictionary from list of dictionaries
- Python | Split dictionary of lists to list of dictionaries
- Python | Sort Flatten list of list
- Ways to sort list of dictionaries by values in Python – Using itemgetter
- Python | Ways to flatten a 2D list
- Ways to sort list of dictionaries by values in Python - Using lambda function
- Python | Flatten Tuples List to String
- Python | Merging two Dictionaries
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



