
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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.
With for 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.
Example
listA = [{'Mon':2}, {'Tue':11}, {'Wed':3}] # printing given arrays print("Given array:\n",listA) print("Type of Object:\n",type(listA)) res = {} for x in listA: res.update(x) # Result print("Flattened object:\n ", res) print("Type of flattened Object:\n",type(res))
Output
Running the above code gives us the following result −
('Given array:\n', [{'Mon': 2}, {'Tue': 11}, {'Wed': 3}]) ('Type of Object:\n', ) ('Flattened object:\n ', {'Wed': 3, 'Mon': 2, 'Tue': 11}) ('Type of flattened Object:\n', )
With reduce
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.
Example
from functools import reduce listA = [{'Mon':2}, {'Tue':11}, {'Wed':3}] # printing given arrays print("Given array:\n",listA) print("Type of Object:\n",type(listA)) # Using reduce and update res = reduce(lambda d, src: d.update(src) or d, listA, {}) # Result print("Flattened object:\n ", res) print("Type of flattened Object:\n",type(res))
Output
Running the above code gives us the following result −
('Given array:\n', [{'Mon': 2}, {'Tue': 11}, {'Wed': 3}]) ('Type of Object:\n', ) ('Flattened object:\n ', {'Wed': 3, 'Mon': 2, 'Tue': 11}) ('Type of flattened Object:\n', )
With ChainMap
The ChainMap function will read each element from the list and create a new collection object but not a dictionary.
Example
from collections import ChainMap listA = [{'Mon':2}, {'Tue':11}, {'Wed':3}] # printing given arrays print("Given array:\n",listA) print("Type of Object:\n",type(listA)) # Using reduce and update res = ChainMap(*listA) # Result print("Flattened object:\n ", res) print("Type of flattened Object:\n",type(res))
Output
Running the above code gives us the following result −
Given array: [{'Mon': 2}, {'Tue': 11}, {'Wed': 3}] Type of Object: Flattened object: ChainMap({'Mon': 2}, {'Tue': 11}, {'Wed': 3}) Type of flattened Object:
- Related Articles
- Flatten Nested List Iterator in Python
- Flatten tuple of List to tuple in Python
- Flatten Tuples List to String in Python
- How to flatten a shallow list in Python?
- Python program to Flatten Nested List to Tuple List
- Python - Ways to flatten a 2D list
- Ways to sort list of dictionaries by values in Python
- Ways to sort list of dictionaries using values in python
- Python – Merge Dictionaries List with duplicate Keys
- Append list of dictionaries to an existing Pandas DataFrame in Python
- How to sort a list of dictionaries by values of dictionaries in C#?
- Python Program to Flatten a Nested List using Recursion
- Python Program to Flatten a List without using Recursion
- Python - Filter dictionaries by values in Kth Key in list
- Ways to sort list of dictionaries by values in Python Using itemgetter

Advertisements