Home / Python / Example programs / Lists

Python program: Remove duplicates from a list

Remove repeated items, keeping the original order.

The code

items = ["b", "a", "b", "c", "a", "d"]

print(list(dict.fromkeys(items)))   # keeps order

seen = set()
unique = []
for x in items:
    if x not in seen:
        seen.add(x)
        unique.append(x)
print(unique)

print(sorted(set(items)))          # a set loses the order; sorted gives a predictable one

Output

['b', 'a', 'c', 'd']
['b', 'a', 'c', 'd']
['a', 'b', 'c', 'd']

How it works

Dictionary keys are unique and remember insertion order, so dict.fromkeys is a neat one-liner. The loop with a set shows the idea.

Python features used

Learn the ideas behind it in the free lesson Dictionaries & Sets.