How do I deduplicate dicts in Python with type hints?

In Python, you can deduplicate dictionaries using various methods. One common approach is to convert dictionaries to a set of tuples (since dictionaries are unhashable) and then back to dictionaries. Here is an example implementation with type hints:

from typing import List, Dict def deduplicate_dicts(dicts: List[Dict]) -> List[Dict]: seen = set() unique_dicts = [] for d in dicts: # Convert the dictionary items to a tuple so it can be added to a set items = frozenset(d.items()) if items not in seen: seen.add(items) unique_dicts.append(d) return unique_dicts # Example usage dict_list = [ {'a': 1, 'b': 2}, {'a': 1, 'b': 2}, {'a': 3, 'b': 4} ] unique = deduplicate_dicts(dict_list) print(unique) # Output: [{'a': 1, 'b': 2}, {'a': 3, 'b': 4}]

deduplicate dictionaries python type hints list dict