Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Flatten given list of dictionaries in Python
We have a list whose elements are dictionaries. We need to flatten it to get a single dictionary where all these list elements are present as key-value pairs.
Using for Loop and update()
We take an empty dictionary and add elements to it by reading the elements from the list. The addition of elements is done using the update() function ?
listA = [{'Mon':2}, {'Tue':11}, {'Wed':3}]
# printing given arrays
print("Given array:", listA)
print("Type of Object:", type(listA))
res = {}
for x in listA:
res.update(x)
# Result
print("Flattened object:", res)
print("Type of flattened Object:", type(res))
Given array: [{'Mon': 2}, {'Tue': 11}, {'Wed': 3}]
Type of Object: <class 'list'>
Flattened object: {'Mon': 2, 'Tue': 11, 'Wed': 3}
Type of flattened Object: <class 'dict'>
Using reduce() with update()
We can also use the reduce() function along with update() function to read the elements from the list and add it to the empty dictionary ?
from functools import reduce
listA = [{'Mon':2}, {'Tue':11}, {'Wed':3}]
# printing given arrays
print("Given array:", listA)
print("Type of Object:", type(listA))
# Using reduce and update
res = reduce(lambda d, src: d.update(src) or d, listA, {})
# Result
print("Flattened object:", res)
print("Type of flattened Object:", type(res))
Given array: [{'Mon': 2}, {'Tue': 11}, {'Wed': 3}]
Type of Object: <class 'list'>
Flattened object: {'Mon': 2, 'Tue': 11, 'Wed': 3}
Type of flattened Object: <class 'dict'>
Using ChainMap
The ChainMap function will read each element from the list and create a new collection object. While it provides dictionary-like access, it's technically a ChainMap object ?
from collections import ChainMap
listA = [{'Mon':2}, {'Tue':11}, {'Wed':3}]
# printing given arrays
print("Given array:", listA)
print("Type of Object:", type(listA))
# Using ChainMap
res = ChainMap(*listA)
# Result
print("Flattened object:", res)
print("Type of flattened Object:", type(res))
# Converting to regular dictionary
print("As regular dict:", dict(res))
Given array: [{'Mon': 2}, {'Tue': 11}, {'Wed': 3}]
Type of Object: <class 'list'>
Flattened object: ChainMap({'Mon': 2}, {'Tue': 11}, {'Wed': 3})
Type of flattened Object: <class 'collections.ChainMap'>
As regular dict: {'Wed': 3, 'Tue': 11, 'Mon': 2}
Using Dictionary Comprehension
A more Pythonic approach using dictionary comprehension with items() ?
listA = [{'Mon':2}, {'Tue':11}, {'Wed':3}]
# Using dictionary comprehension
res = {k: v for d in listA for k, v in d.items()}
print("Given array:", listA)
print("Flattened object:", res)
print("Type of flattened Object:", type(res))
Given array: [{'Mon': 2}, {'Tue': 11}, {'Wed': 3}]
Flattened object: {'Mon': 2, 'Tue': 11, 'Wed': 3}
Type of flattened Object: <class 'dict'>
Comparison
| Method | Readability | Performance | Best For |
|---|---|---|---|
| for + update() | High | Good | Beginners, clear logic |
| reduce() | Medium | Good | Functional programming style |
| ChainMap | Medium | Memory efficient | Multiple dict lookup without merging |
| Dict comprehension | High | Best | Pythonic, concise code |
Conclusion
Dictionary comprehension is the most Pythonic and efficient method for flattening a list of dictionaries. Use for + update() for better readability in complex scenarios.
