ZO — Good search should be free.

Dedupe a List, Keep Order

Asked: python function to remove duplicates from a list but keep order

Python function, working code first

Use `dict.fromkeys()` — dictionaries keep insertion order in Python 3.7+, so the keys come out deduplicated in the original order. [2][3]

set(items) removes duplicates but loses the original order, so the result is unpredictable. [1][6]

Why not just use set()?

It builds a dict whose keys are the list items. Duplicate keys collapse, and since dicts preserve insertion order, the first occurrence of each item stays in place. [2][3]

How dict.fromkeys() works

If you must transform or filter items as you go, use a loop with a seen set instead — it is clearer and still order-preserving. [8]

When you need extra processing

Use OrderedDict.fromkeys(items) to guarantee order, then cast back to a list. [5][8]

Older Python (below 3.7)

Open this in ZO