Imagine you’ve just pulled a list of customer email addresses from your database to send out a promotional newsletter. You run the Python script, hit send, and half your customers get the same email twice. That’s the classic duplicate data problem, and it happens more often than you’d think.
When you work with real-world data, sales records, survey responses, API results, or CSV imports, duplicate values sneak in constantly. Python gives you several clean, fast ways to remove duplicates from an array (or list) without writing complex logic from scratch.
In this article, I’ll show you 5 ways to remove duplicates from an array in Python.
The Dataset We’ll Use
Throughout this article, we’ll work with one consistent dataset — a list of product IDs from an e-commerce order log. Some IDs appear more than once because customers ordered the same product multiple times:
order_ids = [101, 203, 305, 101, 407, 203, 509, 305, 101, 407]
Our goal: get a clean list with each product ID appearing only once.
Method 1 – Use set() (Fastest, Order Not Preserved)
Use this method when you only care about unique values and don’t need the original order. It’s the fastest approach and requires zero imports.
Step 1: Pass your list into the built-in set() function. A set is a Python data structure that automatically stores unique values — no duplicates allowed.
Step 2: Convert the result back to a list using list(), since sets aren’t indexed like lists.
# Method 1: Using set()
order_ids = [101, 203, 305, 101, 407, 203, 509, 305, 101, 407]
unique_ids = list(set(order_ids))
print(unique_ids)
Output:
[101, 203, 305, 407, 509] # Order may vary
You can see the output in the screenshot below.

How does this code work?
Python’s set() function takes the list and instantly drops any value it has seen before. Because sets are unordered by nature, the output order is not guaranteed. Wrapping it in list() converts it back to a standard Python list.
Pro Tip: Use this method when you’re working with large lists and raw speed is your priority. If order matters — for example, you need to keep the first occurrence of each ID — skip to Method 2 or Method 3.
Method 2 – Use dict.fromkeys() (Order Preserved, No Libraries)
Use this method when you want unique values and need to keep the original order. It works with pure Python — no external libraries needed.
Step 1: Call dict.fromkeys() on your list. This creates a dictionary where each list element becomes a unique key (dictionaries can’t have duplicate keys).
Step 2: Wrap it in list() to convert the dictionary keys back into a list.
# Method 2: Using dict.fromkeys()
order_ids = [101, 203, 305, 101, 407, 203, 509, 305, 101, 407]
unique_ids = list(dict.fromkeys(order_ids))
print(unique_ids)
Output:
[101, 203, 305, 407, 509]
You can see the output in the screenshot below.

How does this code work?
dict.fromkeys() builds a new dictionary using the list items as keys. Since Python 3.7+, dictionaries maintain insertion order — so the first occurrence of each value is kept, and subsequent duplicates are silently dropped. Converting back with list() gives you a clean, ordered result.
Pro Tip: This is the go-to method when order matters and you want clean, readable one-liner code. It’s faster than a manual loop and more predictable than
set().
Method 3 – Use a for Loop with a seen Set (Order Preserved, Explicit Control)
Use this method when you want full control over the logic — useful in interviews, custom filtering, or when you need to add extra conditions (like skipping None values).
Step 1: Create an empty seen set to track elements you’ve already encountered.
Step 2: Create an empty result list to store unique values.
Step 3: Loop through the original list. For each element, check if it’s already in seen. If not, add it to both seen and result.
# Method 3: Using a for loop with a seen set
order_ids = [101, 203, 305, 101, 407, 203, 509, 305, 101, 407]
seen = set()
result = []
for item in order_ids:
if item not in seen:
seen.add(item)
result.append(item)
print(result)
Output:
[101, 203, 305, 407, 509]
You can see the output in the screenshot below.

How does this code work?
The seen set acts as a memory — it records every value the loop has visited. The not in check runs in O(1) time for sets (extremely fast), so this method scales well even for large lists. The result list collects only the first occurrence of each value.
Pro Tip: Use this approach when you need to add custom logic inside the loop — for example, skipping
Nonevalues, filtering by type, or logging which duplicates were removed.
Method 4 – Use numpy.unique() (Best for Numerical Arrays)
Use this method when your data is numerical (integers or floats) and you’re already working in a data science or scientific computing environment. NumPy is a powerful Python library built for fast array operations.
Step 1: Install NumPy if you haven’t already by running pip install numpy in your terminal.
Step 2: Import NumPy at the top of your script using the standard alias np.
Step 3: Call np.unique() on your array. This returns a sorted NumPy array of unique values.
Step 4: Convert the result back to a Python list with tolist() if needed.
# Method 4: Using numpy.unique()
import numpy as np
order_ids = [101, 203, 305, 101, 407, 203, 509, 305, 101, 407]
unique_ids = np.unique(order_ids).tolist()
print(unique_ids)
Output:
[101, 203, 305, 407, 509]
How does this code work?
np.unique() converts the list to a NumPy array internally, sorts it, and then removes consecutive duplicates in a single optimized pass. The .tolist() method at the end converts the NumPy array back to a standard Python list. Note that the output is always sorted in ascending order.
Pro Tip: This method shines when you’re processing thousands or millions of numerical values. It’s significantly faster than pure Python loops for large datasets. However, it always sorts the output — so don’t use it if preserving the original order is critical.
Method 5 – Use pandas.Series.unique() (Best for DataFrames and CSV Data)
Use this method when you’re already working with pandas — for example, cleaning a column in a DataFrame loaded from a CSV file. Pandas is the most popular Python library for data analysis.
Step 1: Install pandas if needed: pip install pandas.
Step 2: Import pandas with the alias pd.
Step 3: Wrap your list in pd.Series() to create a pandas Series object — think of it as a labeled column of data.
Step 4: Call .unique() on the Series. This returns a NumPy array of unique values in the original order.
Step 5: Convert to a Python list using .tolist().
# Method 5: Using pandas Series.unique()
import pandas as pd
order_ids = [101, 203, 305, 101, 407, 203, 509, 305, 101, 407]
unique_ids = pd.Series(order_ids).unique().tolist()
print(unique_ids)
Output:
[101, 203, 305, 407, 509]
How does this code work?
pd.Series() wraps your list into a pandas Series, which is essentially a one-dimensional labeled array. The .unique() method scans the Series and returns each value the first time it appears — preserving order, unlike np.unique(). The final .tolist() call converts it back to a plain Python list.
Pro Tip: If you’re already using pandas to read a CSV with
pd.read_csv(), you can call.unique()directly on any DataFrame column:df['order_id'].unique(). No need to convert to a list first.
Quick Method Comparison
| Method | Preserves Order | Requires Library | Best For |
|---|---|---|---|
set() | ❌ No | None | Speed, order doesn’t matter |
dict.fromkeys() | ✅ Yes | None | Order + no dependencies |
for loop + seen set | ✅ Yes | None | Custom logic, interviews |
numpy.unique() | ❌ Sorted | NumPy | Large numerical arrays |
pandas.Series.unique() | ✅ Yes | Pandas | DataFrame/CSV workflows |
Things to Keep in Mind
- set() does not preserve order: If you pass
[3, 1, 2, 1]throughset(), you might get back[1, 2, 3]or any other order. Never rely onset()when sequence matters. - dict.fromkeys() requires Python 3.7+: In Python 3.6 and earlier, dictionaries did not guarantee insertion order. If you’re on an older version (which you really shouldn’t be), use the
forloop method instead. - np.unique() always sorts the output: This is a common source of confusion. If you expect
[101, 203, 305, 407, 509]but get a differently ordered result after using NumPy, this is why. - Empty lists are safe: All five methods handle an empty list
[]gracefully — they simply return an empty list without throwing an error. - Mixed data types can cause issues: If your list contains both integers and strings (e.g.,
[1, "1", 2]), Python treats1and"1"as different values.np.unique()may raise aTypeErroron mixed-type lists. Nonevalues are valid duplicates: All methods correctly deduplicate lists that containNone. For example,[None, 1, None, 2]becomes[None, 1, 2]— but withnp.unique(), it may raise an error depending on the data types present.
You now have 5 solid methods to remove duplicates from an array in Python — from zero-dependency pure Python approaches to powerful library-based solutions. For most everyday tasks, use dict.fromkeys() when order matters or set() when it doesn’t; switch to NumPy or pandas when you’re already in a data analysis workflow. I hope you found this article helpful.
You may also like to read:
- Write an Array to a File in Python
- Create Arrays in Python
- Compare Lists, Tuples, Sets, and Dictionaries in Python
- Save an Array to a File in Python

Bijay Kumar is an experienced Python and AI professional who enjoys helping developers learn modern technologies through practical tutorials and examples. His expertise includes Python development, Machine Learning, Artificial Intelligence, automation, and data analysis using libraries like Pandas, NumPy, TensorFlow, Matplotlib, SciPy, and Scikit-Learn. At PythonGuides.com, he shares in-depth guides designed for both beginners and experienced developers. More about us.