Sets
A set is an unordered collection of unique values. Sets shine at two things: removing duplicates and answering "is this in here?" very fast. They also support classic math operations like union and intersection.
What You'll Learn
- Creating sets and removing duplicates
- Adding and removing elements
- Membership testing
- Union, intersection, and difference
Creating Sets
Sets use curly braces, but unlike dictionaries they hold single values, not pairs. Duplicates disappear automatically:
Loading Python Playground...
Loading Python Exercise...
Adding and Removing
Loading Python Playground...
Membership Testing
Checking in on a set is very fast, even for huge collections. This is the main reason to reach for a set:
Loading Python Playground...
Loading Python Exercise...
Set Math
Loading Python Playground...
Loading Python Exercise...
A Practical Example
Loading Python Playground...
Key Takeaways
- Sets hold unique values with no order:
{1, 2, 3} set(list)removes duplicates instantlyadd()anddiscard()change a setinon a set is very fast|union,&intersection,-difference
Quiz
Question 1 of 425% Complete
0 of 4 questions answered

