🚀 Supercharge your YouTube channel's growth with AI.
Try YTGrowAI FreeHow to Remove Duplicates from List in Python

Removing duplicates from a Python list means keeping one representative of each group of equal values. For values that can be used as dictionary keys, dict.fromkeys() keeps their first-seen order.
In this piece, I’ll show how to keep the first copy and what to use for nested lists.
TL;DR: Keep the First Copy
Use list(dict.fromkeys(items)) to remove repeated hashable values while keeping their first-seen order.
- Choose set(items) when order does not matter.
- Choose a loop with list membership for unhashable values such as nested lists.
- Keep the original list unchanged by assigning the result to a new variable.
What Does Removing Duplicates Mean?
Removing duplicates means grouping equal values and keeping one representative from each group. A Python list is ordered, mutable, and can contain repeated values.
For a refresher on indexes and list operations, see Python list basics.
How to Remove Duplicates Step by Step
The code below works with hashable values and demonstrates what changes when the input is empty or contains nested lists.
Step 1: Pass the values through dict.fromkeys()
Calling dict.fromkeys(items) makes one key per distinct value, then list() returns those keys as a list. Python guarantees dictionary insertion order from version 3.7, which is why each value’s first appearance sets its position.
items = ["pear", "plum", "pear", "apple", "plum"]
print(list(dict.fromkeys(items)))
empty = []
print(list(dict.fromkeys(empty)))
nested = [[1, 2], [1, 2], [3, 4]]
try:
print(list(dict.fromkeys(nested)))
except TypeError as error:
print(type(error).__name__ + ": " + str(error))
unique_nested = []
for item in nested:
if item not in unique_nested:
unique_nested.append(item)
print(unique_nested)
Running python3 dedupe_demo.py prints:
['pear', 'plum', 'apple']
[]
TypeError: unhashable type: 'list'
[[1, 2], [3, 4]]

The first line shows that the second pear and plum disappear without moving apple ahead of plum. An empty input produces an empty list, while TypeError marks a different data shape.
Step 2: Use a set when order does not matter
A set stores distinct hashable values and is handy when the result’s order is irrelevant. Convert it back with list(set(items)) if a list is required. A set does not preserve the source list’s order, so avoid it when later code depends on first-seen order.
Step 3: Compare values when items are unhashable
Lists cannot serve as set members or dictionary keys because they are mutable and unhashable. In my run, the nested-list input raised TypeError: unhashable type: ‘list’. An equality loop returned [[1, 2], [3, 4]].
Separate inner lists with the same elements compare equal even when they are different objects.
Separately created strings with the same text compare equal, which is useful for data loaded from files where matching records can be distinct objects.
Be precise about what counts as equal in application data. Python treats values such as 1 and True as equal dictionary keys, so they collapse into a single entry. If a program needs to distinguish values of different types, transform each item into an explicit comparison key that includes its type as well as its value.
When later copies contain additional information, deleting them may discard data that should instead be merged. Build a mapping that combines matching records according to the application’s rules rather than removing occurrences blindly.
Edge Cases: Order, Hashability, and Cost
Pick a method by checking whether order matters and whether every item is hashable.
Order matters when the list represents a sequence people recognize, such as names in display order or workflow steps. It may not matter when values are used only for membership checks. Decide before converting to a set, since that conversion loses sequence information.
Hashability differs from equality. Dictionary keys and set members need stable hash values while stored, which mutable built-in containers do not provide.
For many unhashable records, choose an immutable field as a key when that field defines a duplicate.
| Input or requirement | Method | Trade-off |
|---|---|---|
| Hashable values, keep first-seen order | list(dict.fromkeys(items)) | Creates a dictionary then a list |
| Hashable values, order irrelevant | list(set(items)) | Result order is unspecified |
| Nested lists or other unhashable values | Loop with item not in result | Repeated equality scans can take longer |
If dictionaries should count as duplicates by a selected field, decide that field before building the result.
A hand-written loop that checks item in result performs repeated scans as the result grows. Avoid removing items from the same list while looping over it, because shifting positions can cause values to be skipped.
Conclusion
Match the method to the values and the job: order, hashability, and whether duplicate records should be merged all affect the right choice.
- Read the official Python dictionary and set documentation for ordering and hashability details.
- See how to convert a string to a list when preparing string data.
FAQ
These answers address common choices that affect a deduplicated result.
How do I remove duplicates while preserving order?
Use list(dict.fromkeys(items)) for hashable values in Python 3.7 or later. It keeps each value’s first occurrence.
Why does dict.fromkeys() raise an unhashable type error?
Dictionary keys must be hashable. Lists and dictionaries are unhashable, so use an equality-based loop or define a hashable key for comparison.
Does set() keep the original list order?
No. A set is unordered, so use dict.fromkeys() when first-seen order matters.

