- Courses
- 💻Programming Languages
- 🐍Python
- Core Data Structures
- Sets
On this page
Sets
By Enrico Hochkoeppler
Web Developer & TutoriaLibre Founder
What a Set Is
A set is an unordered collection of unique elements. Sets automatically remove duplicates, support extremely fast membership testing, and provide powerful mathematical operations such as unions and intersections.
Sets are ideal when you need to ensure uniqueness or perform fast lookups.
A set is written using curly braces:
fruits = {"apple", "banana", "cherry"}
Order is not preserved, and duplicates are removed automatically.
numbers = {1, 2, 2, 3}
print(numbers) # {1, 2, 3}
Creating Sets
You can create sets in multiple ways:
empty = set() # correct way to create an empty set
values = {1, 2, 3}
converted = set([1, 2, 2, 3]) # duplicates removed
Using {} without elements creates a dictionary, not a set.
Set Characteristics
Sets have three defining properties:
- Unordered — no guaranteed order
- Unique elements — duplicates removed
- Mutable container — you can add or remove items
However, set elements themselves must be immutable (numbers, strings, tuples).
Membership Testing
Sets are optimized for extremely fast membership checks.
fruits = {"apple", "banana", "cherry"}
print("banana" in fruits) # True
print("orange" in fruits) # False
This is one of the main reasons sets are used in performance‑critical code.
Adding and Removing Elements
Sets are mutable, so you can modify them.
Adding
items = {1, 2}
items.add(3)
Removing
items.remove(2) # raises error if missing
items.discard(10) # safe: does nothing if missing
Removing and Returning an Arbitrary Element
value = items.pop()
Because sets are unordered, pop() removes an arbitrary element.
Set Operations (Mathematical Operations)
Python sets support classical set theory operations. These are essential for comparing and combining collections.
Union
All elements from both sets.
a = {1, 2}
b = {2, 3}
print(a | b) # {1, 2, 3}
Intersection
Elements common to both sets.
print(a & b) # {2}
Difference
Elements in one set but not the other.
print(a - b) # {1}
Symmetric Difference
Elements in either set, but not both.
print(a ^ b) # {1, 3}
These operations are extremely powerful for filtering, comparing, and analyzing data.
Looping Through Sets
You can iterate through a set using a for loop, but remember that order is not guaranteed.
for item in {"a", "b", "c"}:
print(item)
Iteration is efficient and commonly used for processing unique values.
When to Use Sets
Use a set when:
- You need to remove duplicates
- You need fast membership testing
- Order does not matter
- You need to perform mathematical operations on collections
- You are comparing or filtering large datasets
Sets are often used in data cleaning, search algorithms, and performance‑critical applications.
Why Sets Matter
Sets provide a clean, efficient way to manage unique data and perform fast lookups. Their mathematical operations make them ideal for comparing collections, filtering results, and solving problems involving relationships between groups of values. Understanding sets completes your foundation in Python’s core data structures and prepares you for more advanced data‑handling techniques.
Found this helpful? Share the knowledge...
... or support this project to keep it alive!
Previous
Dictionaries
Next
Nested Structures
-
What a Set Is
-
Creating Sets
-
Set Characteristics
-
Membership Testing
-
Adding and Removing Elements
- Adding
- Removing
- Removing and Returning an Arbitrary Element
-
Set Operations (Mathematical Operations)
- Union
- Intersection
- Difference
- Symmetric Difference
-
Looping Through Sets
-
When to Use Sets
-
Why Sets Matter
