Order-preserving deduplication with dict.fromkeys
The idiomatic Python 3.7+ solution is list(dict.fromkeys(items)). A dict preserves insertion order and keys are unique, so the first occurrence of each hashable item is kept in order. If items are not hashable, you cannot use a dict or set directly; you need a different equality strategy, such as serializing to a hashable key or using an O(n^2) comparison fallback.
dict.fromkeys is concise and preserves first-occurrence order.
A seen set plus list append is also O(n) and can be faster if you need custom filtering logic.
Trade-off: dict.fromkeys builds a dict, while seen-set builds a set. Both are O(n) average time and O(n) space.
Common mistake: using list(set(items)), which removes duplicates but destroys order.
Version note: dict.fromkeys order preservation relies on Python 3.7+ dict insertion order guarantee.
0-2 years experience
2-5 years experience
5-8 years experience
8+ years experience