Python Remove Duplicates from List of Dicts
Author:CodeSnippets
Language:
Python
6 views
0 favorites
2 hours ago
Code Implementation
Python
def unique_dicts(data, key):
seen = set()
result = []
for d in data:
if d[key] not in seen:
seen.add(d[key])
result.append(d)
return result
# Usage
data = [
{"id": 1, "name": "Alice"},
{"id": 1, "name": "Alice"},
{"id": 2, "name": "Bob"}
]
print(unique_dicts(data, "id"))
Remove duplicates from a list of dictionaries based on a specific key in the dictionaries.
#dict#deduplicate
Snippet Description
- Use a set to record the keys that have been seen
- Ensure that only the first record with the same key is retained in the result
- Often used to deduplicate database query results
Recommended Snippets
Python Read Nested JSON File Example
How to read JSON files with nested structures in Python and safely access deep-level fields.
Python
#json+1
8
0
Python read JSON from file example
The simplest code snippet for reading a JSON file using Python, implemented with the built-in json module.
Python
#json
12
0
Python: Write Dict to CSV with Headers
Write a list of dictionaries to a CSV file and automatically generate headers.
Python
#dict+1
6
0
Comments
Loading...