set(): Remove Duplicates Easily
Published:
• Last updated:
• By Jeferson Peter
Python
Remove duplicates from a list
nums = [1, 2, 2, 3, 4, 4, 5]
unique = set(nums)
print(unique)
# {1, 2, 3, 4, 5}
Back to list
nums = [1, 2, 2, 3, 4, 4, 5]
unique_list = list(set(nums))
print(unique_list)
# [1, 2, 3, 4, 5]
Preserve order (Python 3.7+)
nums = [3, 1, 2, 3, 2, 1]
unique_ordered = list(dict.fromkeys(nums))
print(unique_ordered)
# [3, 1, 2]
Conclusion
Using set()
is the fastest way to remove duplicates, but if you also need to preserve the original order, use dict.fromkeys()
.