Python Basics — Set Operations
Set Operations
Python sets support mathematical set operations: union, intersection, difference, and symmetric difference.
Union (|)
All elements from both sets:
a = {1, 2, 3}
b = {3, 4, 5}
print(a | b) # {1, 2, 3, 4, 5}
print(a.union(b)) # {1, 2, 3, 4, 5}
Intersection (&)
Elements common to both sets:
a = {1, 2, 3, 4}
b = {3, 4, 5, 6}
print(a & b) # {3, 4}
print(a.intersection(b)) # {3, 4}
Difference (-)
Elements in the first set but not in the second:
a = {1, 2, 3, 4}
b = {3, 4, 5, 6}
print(a - b) # {1, 2}
print(a.difference(b)) # {1, 2}
print(b - a) # {5, 6} (order matters!)
Symmetric Difference (^)
Elements in either set, but not in both:
a = {1, 2, 3, 4}
b = {3, 4, 5, 6}
print(a ^ b) # {1, 2, 5, 6}
print(a.symmetric_difference(b)) # {1, 2, 5, 6}
Visual Summary
a = {1, 2, 3, 4}
b = {3, 4, 5, 6}
Union (a | b): {1, 2, 3, 4, 5, 6} — everything
Intersection (a & b): {3, 4} — overlap
Difference (a - b): {1, 2} — only in a
Sym Diff (a ^ b): {1, 2, 5, 6} — not shared
Operator vs Method Syntax
| Operation | Operator | Method |
|---|---|---|
| Union | a \| b |
a.union(b) |
| Intersection | a & b |
a.intersection(b) |
| Difference | a - b |
a.difference(b) |
| Symmetric Diff | a ^ b |
a.symmetric_difference(b) |
The method syntax accepts any iterable, while operators require sets:
a = {1, 2, 3}
# Method accepts iterable
print(a.union([3, 4, 5])) # {1, 2, 3, 4, 5}
# Operator requires set
# print(a | [3, 4, 5]) # TypeError
print(a | set([3, 4, 5])) # {1, 2, 3, 4, 5}
Subset and Superset
a = {1, 2}
b = {1, 2, 3, 4}
# Subset: all elements of a are in b
print(a <= b) # True
print(a.issubset(b)) # True
# Proper subset (subset and not equal)
print(a < b) # True
# Superset: b contains all elements of a
print(b >= a) # True
print(b.issuperset(a)) # True
# Proper superset
print(b > a) # True
# Equal sets
print({1, 2} <= {1, 2}) # True (subset)
print({1, 2} < {1, 2}) # False (not proper subset)
Disjoint Sets
Two sets with no common elements:
a = {1, 2, 3}
b = {4, 5, 6}
c = {3, 4, 5}
print(a.isdisjoint(b)) # True (no overlap)
print(a.isdisjoint(c)) # False (3 is common)
Real-World Use Cases
Finding Common Interests
alice_hobbies = {"reading", "hiking", "cooking", "chess"}
bob_hobbies = {"gaming", "hiking", "cooking", "cycling"}
common = alice_hobbies & bob_hobbies
print(f"Common hobbies: {common}")
# Common hobbies: {'hiking', 'cooking'}
only_alice = alice_hobbies - bob_hobbies
print(f"Alice's unique: {only_alice}")
# Alice's unique: {'reading', 'chess'}
Deduplication Check
emails = ["a@x.com", "b@x.com", "a@x.com", "c@x.com"]
unique = set(emails)
duplicates = len(emails) - len(unique)
print(f"Found {duplicates} duplicate(s)") # Found 1 duplicate(s)
Permission Checking
required = {"read", "write"}
user_perms = {"read", "write", "execute"}
if required <= user_perms:
print("Access granted") # Access granted
else:
missing = required - user_perms
print(f"Missing permissions: {missing}")
Multiple Set Operations
a = {1, 2, 3}
b = {2, 3, 4}
c = {3, 4, 5}
# Union of multiple sets
print(a | b | c) # {1, 2, 3, 4, 5}
print(a.union(b, c)) # {1, 2, 3, 4, 5}
# Intersection of multiple sets
print(a & b & c) # {3}
print(a.intersection(b, c)) # {3}
Summary
- Union (
|): all elements from both sets - Intersection (
&): elements common to both - Difference (
-): elements in first but not second - Symmetric difference (
^): elements in either but not both - Subset (
<=), superset (>=), disjoint (.isdisjoint()) - Methods accept any iterable; operators require sets
Next up: Set Methods →