You probably know that Python sets are great for storing unique values and checking membership fast. But sets are far more powerful when you start using advanced operations like unions, intersections, symmetric differences, and more. Let's dive deep into what makes sets an underrated gem in Python's standard library.
π¦ Quick Refresher: What is a Set?
A set is an unordered, mutable, and duplicate-free collection:
a = {1, 2, 3}
b = set([3, 4, 5])
Sets are perfect when:
- You want to remove duplicates
- You care about fast membership checks
- You want to compare data collections
π 1. Union: | or .union()
Combine elements from both sets β no duplicates.
a = {1, 2, 3}
b = {3, 4, 5}
print(a | b) # {1, 2, 3, 4, 5}
Or:
a.union(b)
π 2. Intersection: & or .intersection()
Get common elements between sets.
print(a & b) # {3}
Or:
a.intersection(b)
π 3. Difference: - or .difference()
Get items in a but not in b.
print(a - b) # {1, 2}
This is order-sensitive: a - b β b - a.
βοΈ 4. Symmetric Difference: ^ or .symmetric_difference()
Elements that are in either set but not both.
print(a ^ b) # {1, 2, 4, 5}
β
5. Subset and Superset
a = {1, 2}
b = {1, 2, 3, 4}
print(a <= b) # True (a is subset of b)
print(b >= a) # True (b is superset of a)
You can also use:
a.issubset(b)
b.issuperset(a)
π« 6. Disjoint Sets
Check if two sets have no elements in common.
a = {1, 2}
b = {3, 4}
print(a.isdisjoint(b)) # True
βοΈ 7. Set Comprehensions
Build sets on the fly:
squares = {x * x for x in range(6)}
print(squares) # {0, 1, 4, 9, 16, 25}
Filter during construction:
evens = {x for x in range(10) if x % 2 == 0}
π₯ 8. Updating Sets In-Place
Mutating operations:
a = {1, 2, 3}
b = {3, 4, 5}
a.update(b) # Like union
a.intersection_update({2, 3, 4}) # Keep only shared items
a.difference_update({3}) # Remove common items
a.symmetric_difference_update({2, 5}) # Replace with symmetric diff
π§ͺ Real-World Example: Finding Unique vs Shared Users
logged_in_users = {'alice', 'bob', 'carol'}
purchased_users = {'carol', 'dave'}
# Who logged in but didn't purchase
print(logged_in_users - purchased_users) # {'alice', 'bob'}
# Who both logged in and purchased
print(logged_in_users & purchased_users) # {'carol'}
# Everyone involved
print(logged_in_users | purchased_users) # {'alice', 'bob', 'carol', 'dave'}
π Pro Tips
- Sets are faster than lists for membership checks (in).
- Use frozenset() when you need an immutable set (e.g., as a dict key).
- Avoid using sets when order matters (use list or OrderedDict instead).
π§ Summary
Operation | Symbol | Method Equivalent |
---|---|---|
Union | a | b | a.union(b) |
Intersection | a & b | a.intersection(b) |
Difference | a - b | a.difference(b) |
Symmetric Difference | a ^ b | a.symmetric_difference(b) |
Subset / Superset | a <= b, a >= b | a.issubset(b), a.issuperset(b) |
Disjoint | (no symbol) | a.isdisjoint(b) |
π Final Thoughts
Python sets are simple but powerful tools for data comparison, filtering, and logic. If you're writing code that needs to compare or filter collections β chances are, sets will make your life easier and your code cleaner.
π¨οΈ Whatβs Next?
Want to explore more?
- Using Counter for multiset behavior
- Building set-based recommendation systems
- Performance benchmarking: list vs set lookup
Follow for more Python deep dives! πβ‘
Top comments (0)