Posts

Showing posts with the label unique dicts

How to get the set of dictionaries from a list of dictionaries with duplicates?



To find the set of dictionaries from a list of dictionaries with duplicates we can use itertools.groupby. The important part is that the list of dictionaries is sorted by the keys of the dicts before applying groupby. In our example this is key='city'. To obtain the list of unique dicts we use only the key part of the tuple returned by the groupby iterator.


Github gist with code

dependencies: python3.9