Python Sets
Sets are one of Python’s built-in data types used to store multiple unique values in a single variable.
They are unordered, mutable, and unindexed, making them ideal for fast membership testing and removing duplicates.
This topic covers sets from basics to practical usage.
What Is a Set?
A set is a collection that:
- Stores unique elements only
- Is unordered
- Is mutable
- Does not allow indexing
python
my_set = {1, 2, 3, 4}
print(my_set)
python
duplicates = {1, 2, 2, 3, 3}
print(duplicates)
Set vs List vs Tuple
| Feature | List | Tuple | Set |
|---|---|---|---|
| Ordered | Yes | Yes | No |
| Allows duplicates | Yes | Yes | No |
| Mutable | Yes | No | Yes |
| Indexing | Yes | Yes | No |
Creating Sets
Using Curly Braces {}
python
fruits = {"apple", "banana", "cherry"}
print(fruits)
Using set() Constructor
python
numbers = set([1, 2, 3, 3])
print(numbers)
python
letters = set("Python")
print(letters)
Empty Set (Important)
python
empty_set = set()
print(type(empty_set))
This is NOT a set:
python
empty = {}
print(type(empty)) # dict
Access Set Items
Sets are unordered, so you cannot access items using indexes.
python
fruits = {"apple", "banana", "cherry"}
for item in fruits:
print(item)
Check If Item Exists
python
fruits = {"apple", "banana", "cherry"}
print("apple" in fruits)
python
print("mango" not in fruits)
Add Items to a Set
Add One Item – add()
python
fruits = {"apple", "banana"}
fruits.add("cherry")
print(fruits)
Add Multiple Items – update()
python
fruits.update(["orange", "mango"])
print(fruits)
python
fruits.update({"grape", "kiwi"})
Remove Items from a Set
remove() – Raises Error if Not Found
python
fruits.remove("banana")
print(fruits)
discard() – No Error if Not Found
python
fruits.discard("pineapple")
print(fruits)
pop() – Removes Random Item
python
item = fruits.pop()
print(item)
print(fruits)
clear() – Remove All Items
python
fruits.clear()
print(fruits)
Set Length
python
numbers = {1, 2, 3, 4}
print(len(numbers))
Join Sets (Set Operations)
Union (| or union())
python
a = {1, 2, 3}
b = {3, 4, 5}
print(a | b)
python
print(a.union(b))
Intersection (& or intersection())
python
print(a & b)
python
print(a.intersection(b))
Difference (- or difference())
python
print(a - b)
python
print(a.difference(b))
Symmetric Difference (^)
python
print(a ^ b)
Set Comprehension
python
numbers = [1, 2, 3, 4, 5]
even_set = {x for x in numbers if x % 2 == 0}
print(even_set)
Frozen Set (Immutable Set)
python
fs = frozenset([1, 2, 3])
print(fs)
Cannot modify:
python
# fs.add(4) # AttributeError
Common Mistakes
Expecting Order in Sets
python
s = {1, 2, 3}
print(s)
Order may change.
Using Mutable Items in Set
python
# s = {[1, 2], 3} # TypeError
When to Use Sets
Use sets when:
- You need unique values
- Membership checking is frequent
- Order does not matter
- Removing duplicates is required
Summary
- Sets store unique values
- Unordered and unindexed
- Mutable and dynamic
- Very fast membership testing
- Support powerful mathematical operations
- Ideal for deduplication and comparisons