Write a function that removes duplicates from a list, while keeping the order each item first appeared in.
unique_preserve_order(items)
unique_preserve_order(["a", "b", "a", "c", "b", "d"]) // → ["a", "b", "c", "d"]
unique_preserve_order(["1", "1", "1", "2", "3", "2"]) // → ["1", "2", "3"]
unique_preserve_order(["x", "y", "z"]) // → ["x", "y", "z"]
The obvious tool for "remove duplicates" is a set — but a set doesn't remember order, so converting to a set and back to a list can scramble the sequence. You need something that's both automatically-unique and order-preserving.
That something is a dict. Since Python 3.7, dict keys are guaranteed to preserve insertion order — and a dict can never have two entries with the same key, so assigning the same key twice just keeps the first position and ignores the second.
def unique_preserve_order(items):
return list(dict.fromkeys(items))
dict.fromkeys(items) builds a dict where every element of items becomes a key (and all values default to None, which you don't care about here). Feeding it ["a", "b", "a", "c", "b", "d"] produces a dict with keys a, b, c, d, in that order — the second "a" and second "b" don't create new entries, since those keys already exist. Wrapping the result in list(...) extracts just the keys as a plain list.
This is a genuinely useful trick worth remembering: a dict is Python's built-in ordered set. Any time you need "unique items, in first-seen order", reach for dict.fromkeys() before writing a manual loop with a seen set and an output list.
def unique_preserve_order(items):
return list(dict.fromkeys(items))
Sign in to join the discussion.
No comments yet. Be the first to ask a question.
Press Run to execute your code, or Submit to test and complete this problem.