python / beginner
Snippet
Working with Sets
Sets are unordered collections of unique elements. They automatically remove any duplicate values you try to add.
snippet.py
1
2
3
unique_numbers = {1, 2, 2, 3, 4, 4}print(unique_numbers)# Output: {1, 2, 3, 4}
Breakdown
1
unique_numbers = {1, 2, 2, 3, 4, 4}
Curly braces define a set. Notice the duplicate 2 and 4.
2
print(unique_numbers)
The output only contains one instance of each number.