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
Python program to update a dictionary with the values from a dictionary list
In Python, dictionaries are powerful data structures that store key-value pairs. Sometimes we need to merge multiple dictionaries from a list into a single dictionary. This operation combines all key-value pairs, with later values overwriting earlier ones for duplicate keys.
This article explores three efficient approaches: using a for loop with update(), dictionary comprehension, and the ChainMap method.
Using For Loop with update() Method
The update() method merges one dictionary into another. We can iterate through a list of dictionaries and update our target dictionary ?
Example
# Create an empty dictionary
target_dict = {}
# Dictionary list
dict_list = [
{"name": "John", "age": 25},
{"city": "New York", "country": "USA"},
{"language": "Python", "level": "Intermediate"}
]
# Update the target dictionary with values from the dictionary list
for dictionary in dict_list:
target_dict.update(dictionary)
# Print the updated target dictionary
print("Updated Dictionary:", target_dict)
Updated Dictionary: {'name': 'John', 'age': 25, 'city': 'New York', 'country': 'USA', 'language': 'Python', 'level': 'Intermediate'}
Using Dictionary Comprehension
Dictionary comprehension provides a concise way to create a new dictionary by flattening all key-value pairs from the dictionary list ?
Example
# Dictionary list
dict_list = [
{"name": "John", "age": 25},
{"city": "New York", "country": "USA"},
{"language": "Python", "level": "Intermediate"}
]
# Create a new dictionary using dictionary comprehension
updated_dict = {key: value for dictionary in dict_list for key, value in dictionary.items()}
# Print the updated dictionary
print("Updated Dictionary:", updated_dict)
Updated Dictionary: {'name': 'John', 'age': 25, 'city': 'New York', 'country': 'USA', 'language': 'Python', 'level': 'Intermediate'}
Using ChainMap
The ChainMap class from the collections module creates a single view of multiple dictionaries without copying data ?
Example
from collections import ChainMap
# Dictionary list
dict_list = [
{"name": "John", "age": 25},
{"city": "New York", "country": "USA"},
{"language": "Python", "level": "Intermediate"}
]
# Create a ChainMap and convert to dictionary
chained = ChainMap(*dict_list)
updated_dict = dict(chained)
print("Updated Dictionary:", updated_dict)
Updated Dictionary: {'language': 'Python', 'level': 'Intermediate', 'city': 'New York', 'country': 'USA', 'name': 'John', 'age': 25}
Comparison
| Method | Memory Usage | Readability | Best For |
|---|---|---|---|
| For Loop + update() | Efficient | High | Complex operations during merge |
| Dictionary Comprehension | Moderate | High | One-liner solutions |
| ChainMap | Very Efficient | Moderate | Large dictionaries, view-only operations |
Handling Duplicate Keys
When dictionaries contain duplicate keys, the last occurrence overwrites previous values ?
dict_list = [
{"name": "John", "age": 25},
{"name": "Jane", "city": "Boston"}, # 'name' will be overwritten
{"age": 30} # 'age' will be overwritten
]
updated_dict = {}
for d in dict_list:
updated_dict.update(d)
print("Final Dictionary:", updated_dict)
Final Dictionary: {'name': 'Jane', 'age': 30, 'city': 'Boston'}
Conclusion
Use for loop with update() for readable code and complex operations. Choose dictionary comprehension for concise one-liners. Consider ChainMap for memory-efficient operations on large datasets.
