How do I deduplicate dicts in Python safely and idiomatically?

To deduplicate dictionaries (dicts) in Python safely and idiomatically, you can use different techniques depending on the structure of the data. Here are some methods to achieve this effectively.

Example of Deduplicating a List of Dictionaries

Suppose you have a list of dictionaries and you want to remove duplicates based on a specific key. Here's how you can do it:

# Sample list of dictionaries data = [ {'id': 1, 'name': 'Alice'}, {'id': 2, 'name': 'Bob'}, {'id': 1, 'name': 'Alice'}, # Duplicate {'id': 3, 'name': 'Charlie'} ] # Using a set to track seen IDs seen = set() unique_data = [] for item in data: if item['id'] not in seen: seen.add(item['id']) unique_data.append(item) print(unique_data) # Output: [{'id': 1, 'name': 'Alice'}, {'id': 2, 'name': 'Bob'}, {'id': 3, 'name': 'Charlie'}]

deduplicate dictionaries Python remove duplicates unique data