❓ Python How-To Guides
Estimated reading: 3 minutes 272 views

Python Remove List Duplicates – Fast and Simple Methods

Introduction – Why Remove Duplicates?

Python lists often contain repeated elements, especially when aggregating or merging data. Removing duplicates is important for:

  • Data cleaning
  • Unique value extraction
  • Optimizing lookups
  • Deduplicating logs, emails, IDs

Python offers several elegant, fast ways to remove duplicates from a list, whether or not you need to preserve order.

In this guide, you’ll learn:

  • How to remove duplicates with set, dict, and loops
  • Preserve or ignore order depending on your use case
  • Use collections, pandas, and list comprehension
  • Best practices for handling duplicates

Quickest Way – Use set() to Remove Duplicates

numbers = [1, 2, 2, 3, 4, 1]
unique = list(set(numbers))
print(unique)  # [1, 2, 3, 4] (order not preserved)

Removes duplicates fast, but order may change.


Preserve Order – Use dict.fromkeys()

names = ["Alice", "Bob", "Alice", "Eve"]
unique = list(dict.fromkeys(names))
print(unique)  # ['Alice', 'Bob', 'Eve']

Python 3.7+ maintains insertion order in dicts.


Loop-Based Approach (Manual Deduplication)

data = ["x", "y", "x", "z", "y"]
unique = []
for item in data:
    if item not in unique:
        unique.append(item)

print(unique)  # ['x', 'y', 'z']

Works in all versions, preserves order.
Slower on large lists due to repeated lookups.


Using collections.OrderedDict (Python <3.7)

from collections import OrderedDict

items = [3, 2, 3, 1, 2]
unique = list(OrderedDict.fromkeys(items))
print(unique)  # [3, 2, 1]

Preserves order before Python 3.7.


Use pandas for List-Like Series

import pandas as pd

values = [5, 3, 5, 2, 1, 2]
unique = pd.Series(values).drop_duplicates().tolist()
print(unique)  # [5, 3, 2, 1]

Useful in data science and analytics workflows.


List Comprehension + Set for Fast Deduplication

seen = set()
unique = [x for x in [1, 1, 2, 3, 2] if not (x in seen or seen.add(x))]
print(unique)  # [1, 2, 3]

Short and efficient. Preserves order.
Uses set.add() inside a comprehension for side effect.


Remove Duplicates from List of Tuples

pairs = [(1, 2), (2, 3), (1, 2)]
unique = list(dict.fromkeys(pairs))
print(unique)  # [(1, 2), (2, 3)]

Works because tuples are hashable.


🔏 Remove Duplicates from List of Dictionaries

import json

people = [{"name": "Alice"}, {"name": "Bob"}, {"name": "Alice"}]
seen = set()
unique = []

for p in people:
    j = json.dumps(p, sort_keys=True)
    if j not in seen:
        unique.append(p)
        seen.add(j)

print(unique)

Use json.dumps() to hash dicts safely.


Best Practices

Do This Avoid This
Use set() for fast deduplicationExpecting order preservation with set()
Use dict.fromkeys() to preserve orderRelying on set() for ordered results
Use json.dumps() for list of dictsUsing dict as keys directly
Profile speed on large datasetsOverusing slow if-in-list in loops

Summary – Recap & Next Steps

Python offers fast and flexible tools to remove duplicates from lists. Your choice depends on whether you need to preserve order, maintain performance, or deduplicate complex structures.

Key Takeaways:

  • Use set() to remove duplicates quickly (unordered)
  • Use dict.fromkeys() to preserve order in Python 3.7+
  • Use list comprehension + set.add() for fast and readable code
  • Use json.dumps() to handle unhashable dicts

Real-World Relevance:
Used in data preprocessing, user input cleaning, log file parsing, deduplicating API responses, and data deduplication pipelines.


FAQ – Python Remove List Duplicates

How do I remove duplicates from a list while keeping order?

Use:

list(dict.fromkeys(your_list))

Why does set() not preserve order?

Because sets are unordered collections (until Python 3.7 dicts preserved insertion order, but sets don’t guarantee it).

Can I remove duplicates from a list of dictionaries?

Yes, convert dicts to strings using json.dumps() and track uniqueness with a set.

What’s the fastest way to remove duplicates?

Use:

list(set(your_list))

Not suitable if you need to preserve order.

Can I remove duplicates from nested lists?

Not directly. Lists are unhashable. Convert to tuple first:

nested = [[1,2], [3,4], [1,2]]
unique = [list(t) for t in dict.fromkeys(map(tuple, nested))]

Share Now :
Share

Python Remove List Duplicates

Or Copy Link

CONTENTS
Scroll to Top