How to Remove Duplicates from a List in Python

Short answer
Use list(dict.fromkeys(items)) to remove duplicates and keep the original order. Use list(set(items)) when order does not matter, because a set drops duplicates but does not promise any order.

Duplicates sneak into lists all the time: repeated sign-ups, the same product added twice, log lines that appear more than once. Python gives you several ways to clean them up, and the right one depends on two questions: does the order of the items matter, and are the items hashable (strings, numbers and tuples are, lists and dictionaries are not)?

This page shows the three approaches you will actually use, explains why each works, and warns you about the traps, such as a set shuffling your data.

Quick facts
Keep orderlist(dict.fromkeys(items))
Order does not matterlist(set(items))
Items must be hashableYes for both one-liners (strings, numbers, tuples)
Changes the original listNo, it builds a new list
Time complexityO(n) on average

Remove duplicates and keep the original order with dict.fromkeys()

Since Python 3.7 a dictionary remembers the order in which keys were first inserted, and a dictionary can hold each key only once. dict.fromkeys(items) therefore creates a dictionary whose keys are your items without repeats, in order of first appearance. Wrapping it in list() turns the keys back into a list.

Python
orders = ["pen", "book", "pen", "ink", "book"]
unique = list(dict.fromkeys(orders))
print(unique)

The first copy of each item is the one that stays.

Remove duplicates with set() when order is not important

A set stores each value once and is very fast at checking whether it has seen a value. Converting a list to a set and back is the shortest way to deduplicate, but the result can come out in a different order than the input, so sort it if you need a predictable result.

Python
ids = [3, 1, 3, 2, 1]
print(sorted(set(ids)))

For sets of strings the order can even change between runs of the same program, so never rely on it.

Remove duplicates from a list of lists or dictionaries

Lists and dictionaries are not hashable, so set() and dict.fromkeys() raise TypeError. You have two choices: convert each inner list to a tuple first, or fall back to a simple loop that checks not in on the result. The loop is O(n squared) but works for any item type and keeps order.

pairs = [[1, 2], [3], [1, 2]]

# option 1: tuples are hashable
unique = [list(t) for t in dict.fromkeys(tuple(p) for p in pairs)]

# option 2: plain loop, works for any items
unique = []
for p in pairs:
    if p not in unique:
        unique.append(p)

Which method should you choose?

MethodKeeps order?Works with unhashable items?Best for
list(dict.fromkeys(items))YesNoMost everyday cases
list(set(items))NoNoLarge lists where order is irrelevant
Loop with not inYesYesSmall lists of lists or dicts
Loop with a seen setYesNoWhen you need extra logic per item

Examples you can run

Press Run, then edit the code and run it again.

Keep the first occurrence of each item

dict.fromkeys() keeps the order in which items first appeared.

Keep the first occurrence of each item
orders = ["pen", "book", "pen", "ink", "book"]
print(list(dict.fromkeys(orders)))
Expected output['pen', 'book', 'ink']

Use a set and sort the result

When only the unique values matter, a set is enough. Sorting makes the output predictable.

Use a set and sort the result
ids = [3, 1, 3, 2, 1]
print(sorted(set(ids)))
print(len(ids) - len(set(ids)), "duplicates removed")
Expected output[1, 2, 3]
2 duplicates removed

Deduplicate a list of lists

Lists cannot go in a set, so check the result list with not in.

Deduplicate a list of lists
pairs = [[1, 2], [3], [1, 2]]
unique = []
for p in pairs:
    if p not in unique:
        unique.append(p)
print(unique)
Expected output[[1, 2], [3]]

Deduplicate case-insensitively

Build the dictionary from lower-cased keys so "Asha" and "asha" count as the same name.

Deduplicate case-insensitively
names = ["Asha", "ben", "asha", "Ben", "Chen"]
unique = {n.lower(): n for n in reversed(names)}
print(list(unique.values())[::-1])
Expected output['Asha', 'ben', 'Chen']

Common mistakes

Expecting set() to keep the order

✗ Wrong

tags = ["c", "a", "c", "b"]
print(list(set(tags)))
The printed order may differ from the input and can change between runs.

✓ Right

tags = ["c", "a", "c", "b"]
print(list(dict.fromkeys(tags)))

A set has no guaranteed order. Use dict.fromkeys() or sort the result.

Putting unhashable items in a set

✗ Wrong

rows = [[1, 2], [1, 2]]
print(set(rows))
TypeError: unhashable type: 'list'

✓ Right

rows = [[1, 2], [1, 2]]
print(set(tuple(r) for r in rows))

Convert each row to a tuple, or use a loop with not in.

Expecting the original list to change

✗ Wrong

items = [1, 1, 2]
list(set(items))
print(items)
[1, 1, 2] (nothing changed)

✓ Right

items = [1, 1, 2]
items = list(dict.fromkeys(items))
print(items)

set() and dict.fromkeys() create new objects. Assign the result, or use slice assignment items[:] = ... to update in place.

How to Remove Duplicates from a List in Python: questions people ask

How do I remove duplicates from a list in Python without changing the order?

Use list(dict.fromkeys(items)). Dictionaries keep keys in insertion order (guaranteed since Python 3.7), so the first copy of each item stays in its original position.

Is converting a list to a set the fastest way to remove duplicates?

Yes, set(items) is usually the fastest because membership checks are O(1), but it loses the order. dict.fromkeys() is almost as fast and keeps the order.

How do I remove duplicates from a list of dictionaries?

Dictionaries are unhashable, so loop through the list and append an item only if item not in unique, or build a key such as tuple(sorted(d.items())) for each dictionary and track the keys you have seen.

Keep learning

Step-by-step lessons:
Python ListsPython SetsPython Dictionaries

Related:
How to Count Occurrences in a List in PythonPython List vs Set: Differences and When to Use EachPython sort() vs sorted(): What Is the Difference?Python List count() Method

More in lists, dicts and sets:
How to Sort a Dictionary by Value in PythonHow to Merge Two Dictionaries in PythonHow to Reverse a List in PythonHow to Flatten a List of Lists in PythonHow to Check if a List Is Empty in PythonHow to Get the Last Element of a List in PythonHow to Loop Through a Dictionary in PythonHow to Count Occurrences in a List in Python See all →