python / beginner
Snippet
Removing Duplicates with a Set
A Set is a data type that only allows unique elements. By converting a list with duplicates into a set, Python automatically filters out all repeating items for you.
snippet.py
1
2
3
4
numbers = [1, 2, 2, 3, 4, 4]unique_numbers = set(numbers)print(list(unique_numbers))
Breakdown
1
unique_numbers = set(numbers)
Converts the list into a set to eliminate duplicate values.
2
print(list(unique_numbers))
Converts the set back to a list for display or further use.