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 – Merge Dictionaries List with duplicate Keys
When working with lists of dictionaries, you often need to merge them while handling duplicate keys. This process involves iterating through corresponding dictionaries and adding keys that don't already exist.
Basic Dictionary List Merging
Here's how to merge two lists of dictionaries, keeping original values for duplicate keys ?
list1 = [{"aba": 1, "best": 4}, {"python": 10, "fun": 15}, {"scala": "fun"}]
list2 = [{"scala": 6}, {"python": 3, "best": 10}, {"java": 1}]
print("First list:")
print(list1)
print("\nSecond list:")
print(list2)
# Merge dictionaries at corresponding positions
for i in range(len(list1)):
existing_keys = list(list1[i].keys())
for key in list2[i]:
if key not in existing_keys:
list1[i][key] = list2[i][key]
print("\nMerged result:")
print(list1)
First list:
[{'aba': 1, 'best': 4}, {'python': 10, 'fun': 15}, {'scala': 'fun'}]
Second list:
[{'scala': 6}, {'python': 3, 'best': 10}, {'java': 1}]
Merged result:
[{'aba': 1, 'best': 4, 'scala': 6}, {'python': 10, 'fun': 15, 'best': 10}, {'scala': 'fun', 'java': 1}]
Alternative Approach Using Dictionary Update
You can also use dictionary comprehension for a more concise solution ?
list1 = [{"name": "Alice", "age": 25}, {"name": "Bob", "city": "NYC"}]
list2 = [{"city": "LA", "job": "Engineer"}, {"age": 30, "job": "Designer"}]
# Create merged list without modifying originals
merged = [
{**dict1, **{k: v for k, v in dict2.items() if k not in dict1}}
for dict1, dict2 in zip(list1, list2)
]
print("Original list1:", list1)
print("Original list2:", list2)
print("Merged result:", merged)
Original list1: [{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'city': 'NYC'}]
Original list2: [{'city': 'LA', 'job': 'Engineer'}, {'age': 30, 'job': 'Designer'}]
Merged result: [{'name': 'Alice', 'age': 25, 'job': 'Engineer'}, {'name': 'Bob', 'city': 'NYC', 'job': 'Designer'}]
How It Works
The merging process follows these steps:
Iterate through corresponding dictionaries at the same index position
Extract existing keys from the first dictionary
Check each key in the second dictionary
Add only non-duplicate keys to preserve original values
The first dictionary takes precedence for duplicate keys
Comparison of Methods
| Method | Modifies Original? | Best For |
|---|---|---|
| Loop + conditional | Yes | In-place modification |
| Dictionary comprehension | No | Creating new merged list |
Conclusion
Use the loop method when you want to modify lists in-place. Use dictionary comprehension when you need to preserve original data and create a new merged result.
