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



filter_none

edit
close

play_arrow

link
brightness_4
code

# 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


Output:

initial dictionary [{'a': 1}, {'b': 2}, {'c': 3}]
result {'b': 2, 'a': 1, 'c': 3}

 

Method #2: Using dict comprehension

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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


Output:

initial dictionary [{'a': 1}, {'b': 2}, {'c': 3}]
result {'a': 1, 'c': 3, 'b': 2}

 
Method #3: Using reduce

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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


Output:

initial dictionary [{'a': 1}, {'b': 2}, {'c': 3}]
result {'a': 1, 'c': 3, 'b': 2}

 
Method #4: Using collections.ChainMap

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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


Output:

initial dictionary [{'a': 1}, {'b': 2}, {'c': 3}]
result ChainMap({'a': 1}, {'b': 2}, {'c': 3})


My Personal Notes arrow_drop_up

Programming freaktech Enthusiast and have interest in learning new upcoming technologies

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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.