0
0
PythonHow-ToBeginner · 3 min read

How to Remove Duplicates Using Set in Python: Simple Guide

You can remove duplicates from a list in Python by converting it to a set, which automatically keeps only unique items. Then, convert the set back to a list if you need the result as a list.
📐

Syntax

To remove duplicates, use the syntax unique_list = list(set(original_list)). Here, set() creates a set of unique elements from original_list, and list() converts it back to a list.

python
unique_list = list(set(original_list))
💻

Example

This example shows how to remove duplicates from a list of numbers using a set.

python
original_list = [1, 2, 2, 3, 4, 4, 5]
unique_list = list(set(original_list))
print(unique_list)
Output
[1, 2, 3, 4, 5]
⚠️

Common Pitfalls

Using a set removes duplicates but does not keep the original order of items. If order matters, this method may not work as expected.

Also, sets only work with hashable items. Trying to use a set on a list of lists will cause an error.

python
original_list = [3, 1, 2, 2, 3]
unique_list = list(set(original_list))
print(unique_list)  # Order may change

# Wrong: unhashable type error
# original_list = [[1, 2], [1, 2]]
# unique_list = list(set(original_list))  # This will raise an error
Output
[1, 2, 3]
📊

Quick Reference

  • Remove duplicates: list(set(your_list))
  • Order not preserved: Use this only if order does not matter
  • Works with hashable items: Items like numbers, strings, and tuples

Key Takeaways

Convert a list to a set to remove duplicates quickly in Python.
Sets do not keep the original order of elements.
Only use sets with hashable items like numbers and strings.
Convert the set back to a list if you need list operations.
For order-preserving duplicate removal, consider other methods.