Python Basics
Python Sets
1. What is a Set?
A set is an unordered collection of unique elements.
- Unordered — insertion order is not maintained
- Unique — no duplicate elements allowed
- Fast lookups — checking if something exists is almost instant (like a dictionary)
x = {1, 2, 3}
2. Creating a Set
With initial values
x = {1, 2, 3}
Empty set — must use set(), NOT {}
x = set() # ✅ empty set
x = {} # ❌ this creates an empty DICTIONARY
⚠️
{}creates a dictionary, not a set. Always useset()for an empty set.
Converting another collection to a set
set([1, 2, 2, 3, 4]) # → {1, 2, 3, 4} (removes duplicates)
set("hello") # → {'h', 'e', 'l', 'o'}
set((1, 2, 2, 3)) # → {1, 2, 3}
3. Key Properties
- Duplicate values are automatically removed
- You can mix data types:
{1, "hello", True, 0.2, (1, 2)} - Cannot add mutable objects — no lists, dicts, or sets inside a set
- Only immutable types are allowed as elements (ints, strings, tuples, etc.)
x = {1, 1, 2, 2, 3}
print(x) # → {1, 2, 3} duplicates removed
4. Basic Operations
Add an element
x.add(4) # use .add(), NOT .append()
Remove an element
x.remove(2) # raises KeyError if element not found
Clear all elements
x.clear() # → set()
Length
len(x)
Check if element exists
3 in x # → True or False (almost instant)
5. Set Operations
Union — all elements from both sets (no duplicates)
x = {1, 2, 3}
y = {2, 3, 4}
x.union(y) # → {1, 2, 3, 4}
x | y # shortcut
Intersection — only elements in BOTH sets
x.intersection(y) # → {2, 3}
x & y # shortcut
Difference — elements in x but NOT in y
x.difference(y) # → {1}
x - y # shortcut
y.difference(x) # → {4} (order matters!)
y - x # shortcut
Symmetric Difference — elements NOT shared between either set
x.symmetric_difference(y) # → {1, 4}
x ^ y # shortcut
Unlike difference, symmetric difference gives the same result regardless of order.
6. Update Methods (modify in place)
These modify the original set rather than returning a new one.
| Method | Effect |
|---|---|
x.update(y) |
Adds all elements of y into x |
x.difference_update(y) |
Removes from x any elements also in y |
x.symmetric_difference_update(y) |
Updates x to be the symmetric difference of x and y |
x = {1, 2, 3}
y = {2, 3, 4}
x.update(y) # x → {1, 2, 3, 4}
x.difference_update(y) # x → {1}
7. Subset & Superset
- Subset — all elements of x are contained in y
- Superset — y contains all elements of x (and possibly more)
x = {1, 2}
y = {1, 2, 3, 4}
x.issubset(y) # → True (x ⊆ y)
y.issuperset(x) # → True (y ⊇ x)
# Shortcuts
x <= y # is x a subset of y?
y >= x # is y a superset of x?
# Proper subset/superset (sets must NOT be equal)
x < y # proper subset
y > x # proper superset
8. Operations Cheat Sheet Table
| Operation | Method | Shortcut | Modifies original? |
|---|---|---|---|
| Union | .union(y) |
x \| y |
❌ |
| Intersection | .intersection(y) |
x & y |
❌ |
| Difference | .difference(y) |
x - y |
❌ |
| Symmetric Difference | .symmetric_difference(y) |
x ^ y |
❌ |
| Update (union) | .update(y) |
— | ✅ |
| Difference update | .difference_update(y) |
— | ✅ |
| Symmetric diff update | .symmetric_difference_update(y) |
— | ✅ |
| Subset check | .issubset(y) |
x <= y |
❌ |
| Superset check | .issuperset(y) |
x >= y |
❌ |
| Proper subset | — | x < y |
❌ |
| Proper superset | — | x > y |
❌ |
9. Practical Example — Detect Duplicate Input
numbers = set()
while True:
num = int(input("Enter a number: "))
if num in numbers:
break # user entered a duplicate — stop
numbers.add(num)
10. Set vs Dictionary
| Set | Dictionary | |
|---|---|---|
| Stores | Keys only | Key-value pairs |
| Use when | You only care if something exists | You care if something exists and want to store info about it |
| Fast lookups | ✅ Yes | ✅ Yes |
| Tracks frequency | ❌ No | ✅ Yes (via values) |
| Tracks order | ❌ No | ❌ No |
Rule of thumb: Use a set when existence is all that matters. Use a dictionary when you need to store something alongside the key (like a count or label).
Cheat Sheet
# Create
s = {1, 2, 3}
s = set() # empty set (not {})
# Add / Remove / Clear
s.add(4)
s.remove(2) # KeyError if missing
s.clear()
# Check / Length
3 in s
len(s)
# Convert to set (removes duplicates)
set([1, 2, 2, 3]) # → {1, 2, 3}
# Set operations
s1 | s2 # union
s1 & s2 # intersection
s1 - s2 # difference
s1 ^ s2 # symmetric difference
# Subset / Superset
s1 <= s2 # subset
s1 >= s2 # superset
s1 < s2 # proper subset
s1 > s2 # proper superset