How to Remove Duplicates from a List in Python
The fastest way to remove duplicates from a list in Python is
dict.fromkeys():
sin_duplicados = list(dict.fromkeys(numeros))
When do you need this?
Working with lists that contain repeated elements is very common in Python: cleaning user data, removing repeated products, or merging results from several sources. In this guide you will learn how to remove duplicates from a list in Python with several methods, keeping or ignoring the original order.
Method 1: Remove duplicates with set()
The fastest way to remove duplicates is to convert the list to a set and back to a list.
numeros = [1, 2, 2, 3, 3, 3, 4]
sin_duplicados = list(set(numeros))
print(sin_duplicados)
Keep in mind that a set does not preserve the original order of the elements.
Method 2: Preserve the order with dict.fromkeys() (recommended)
If the order matters, use dict.fromkeys(), which keeps the first appearance of each element.
numeros = [1, 2, 2, 3, 3, 3, 4]
sin_duplicados = list(dict.fromkeys(numeros))
print(sin_duplicados)
# [1, 2, 3, 4]
Method 3: Preserve the order with a for loop
You can also build the result manually with a loop. It works with any type of element, including strings.
nombres = ["ana", "luis", "ana", "carla"]
sin_duplicados = []
for nombre in nombres:
if nombre not in sin_duplicados:
sin_duplicados.append(nombre)
print(sin_duplicados)
# ['ana', 'luis', 'carla']
Method 4: Remove duplicates from a list of strings
The same techniques work for text lists, very useful when cleaning user data.
palabras = ["hola", "mundo", "hola", "python"]
sin_duplicados = list(dict.fromkeys(palabras))
print(sin_duplicados)
# ['hola', 'mundo', 'python']
Method comparison
| Method | Advantage | When to use it |
|---|---|---|
set() |
Fastest | When order does not matter |
dict.fromkeys() |
Keeps the order | Recommended in most cases |
for loop |
Works with any element | Educational or special cases |
Common errors and solutions
Losing the order with set()
A set is an unordered collection. If you need the original order, use dict.fromkeys() instead of set().
Using the method on a list of dictionaries
Dictionaries are not hashable, so set() and dict.fromkeys() fail with them. Group by a key instead, for example with a loop that compares a specific field.
Frequently asked questions
Does set() keep the original order?
No. A set is an unordered collection. Use dict.fromkeys() if you need to preserve the order.
Can I remove duplicates without changing the list?
Yes. All the examples above create a new list. The original list is never modified.
Does this work with a list of dictionaries?
Not directly, because dictionaries are not hashable. You need a different approach, for example grouping by a key.
Related content
- How to Sort a Dictionary in Python
- How to Reverse a String in Python
- Python How-To Tutorials
- Python Course from Scratch
Related keywords
how to remove duplicates from a list in python, python remove duplicates, deduplicate list python, set python, dict.fromkeys python, unique values python list, list python
Save this tutorial and share it if it helped you. Find more practical guides in the Python how-to section.