Python Tutorial Removing Duplicate Dictionary Entries from a List Code Example

PHOTO EMBED

Sun Jun 23 2024 18:05:13 GMT+0000 (Coordinated Universal Time)

Saved by @freepythoncode ##python #coding #python #programming #list

myData = [
    {'a': 1},
    {'a': 1},
    {'b': 1},
    {'b': 1},
    {'c': 1},
    {'c': 1},
]

# Use a set to track seen tuples of items (key-value pairs as tuples)
seen = set()
unique_data = []

for d in myData:
    # Convert each dictionary to a tuple of its items, then convert to frozenset
    # to make it hashable and suitable for use in a set
    dict_items = frozenset(d.items())
    
    # Check if the frozenset of items is already in the set
    if dict_items not in seen:
        # If not seen, add to both set and result list
        seen.add(dict_items)
        unique_data.append(d)

print(unique_data)
content_copyCOPY