PythonMastery

Remove duplicate dictionaries from a list in Python

Remove repeated dictionaries from a list while keeping the original order, either by one key such as id or by comparing every field.

A dictionary cannot go into a set, because it can change. So set(records) fails with TypeError: unhashable type: 'dict'. Instead, keep a set of something that identifies each record, and skip any record you have already seen.

The program

python
records = [
    {"id": 1, "name": "Ada"},
    {"id": 2, "name": "Linus"},
    {"id": 1, "name": "Ada"},
    {"id": 3, "name": "Grace"},
    {"id": 2, "name": "Linus"},
]

seen = set()
unique = []
for record in records:
    if record["id"] not in seen:
        seen.add(record["id"])
        unique.append(record)

for record in unique:
    print(record)
output
{'id': 1, 'name': 'Ada'}
{'id': 2, 'name': 'Linus'}
{'id': 3, 'name': 'Grace'}

How it works

Comparing every field

If two records are duplicates only when all their fields match, use the whole record as the identity. tuple(sorted(record.items())) turns a dictionary into a tuple of pairs, which can go into a set:

python
records = [
    {"id": 1, "name": "Ada"},
    {"id": 1, "name": "Ada Lovelace"},
    {"name": "Ada", "id": 1},
]

seen = set()
unique = []
for record in records:
    key = tuple(sorted(record.items()))
    if key not in seen:
        seen.add(key)
        unique.append(record)

print(unique)
output
[{'id': 1, 'name': 'Ada'}, {'id': 1, 'name': 'Ada Lovelace'}]

The first and third records have the same keys and values in a different order, so they count as the same. sorted() puts the pairs in the same order first. This works only when every value is hashable (strings, numbers, tuples). A record with a list inside would raise a TypeError.

Learn the idea: Dictionaries, Sets: Unordered, Unique, Lightning Fast, Lists & Sequences