De-duplicate with sets & dict.fromkeys

De-duplicate with sets & dict.fromkeys

April 16, 2025

Saw this tip from Trey Hunner’s newsletter.

Do you need to de-duplicate items in a list?

>>> my_items = ["duck", "mouse", "duck", "computer"]

If you don’t care about maintaining the order of your items, you could use the built-in set constructor:

>>> unique_items = set(my_items)
>>> unique_items
{'computer', 'mouse', 'duck'}

If you do care about maintaining the order of your items, you could use the dict class’s fromkeys method along with the built-in list constructor:

>>> unique_items = list(dict.fromkeys(my_items))
>>> unique_items
['duck', 'mouse', 'computer']

Note that those two techniques only work for numbers, strings, or other hashable objects.