capypad
0 day streak
python / beginner
Snippet

Removing Duplicates with set()

A set is an unordered collection of unique elements. Converting a list to a set is the easiest way to remove all duplicate values from that list.

snippet.py
python
1
2
3
4
numbers = [1, 2, 2, 3, 4, 4]
unique_numbers = set(numbers)
 
print(unique_numbers)
Breakdown
1
unique_numbers = set(numbers)
Creates a new set object containing only the unique values from the list.
2
print(unique_numbers)
Displays the collection without the repeated numbers.