8 Python Set Things I Regret Not Knowing Earlier
1) Frozensets
Frozensets are like normal sets, but immutable. Which means that after we create a frozenset, we cannot add or delete anything from it.
How we create a frozenset:
Note — checking if an element exists in a frozenset still takes constant time O(1)
The main advantage frozensets have over sets is that it is immutable, which means:
frozensets can be used as dictionary keys (normal sets cannot)
frozensets can be added into another set/frozenset (normal sets cannot)
Otherwise, it behaves exactly like a normal set would.
2) Set comprehension
Like list/dict comprehension, we can use set comprehension to create a set elegantly from another iterable in one line of code in Python.
Notice that this is similar to dictionary comprehension — just that instead of a key-value pair separated by a colon, we put a single value.
3) Union and Intersection
Let’s say we have 2 simple sets — a and b
Let’s say we have 2 simple sets — a and b
The union of 2 sets is a set of all object in both sets. To find the union of sets a and b, we can use a.union(b). Alternatively, we can also use a | b.
The intersection of 2 sets is a set containing elements that appear in both sets. To find the intersection of a and b, we can use a.intersection(b). Alternatively, we can use a & b.
For myself, I personally prefer using the | and & operators to get the unions and intersections of sets over the .union() and .intersection() methods due to brevity, but both are correct and legal.
4) Difference
Let’s say we have 2 simple sets — a and b
The difference between sets a and b — notated by (a-b) — contains elements found in a but not in b.
The difference between sets b and a — notated by (b-a) — contains elements found in b but not in a.
Note — (a-b) and (b-a) are not the same. Order matters.
To find the difference in 2 sets a and b, we can use a.difference(b). Alternatively, we can use the subtraction operator a-b.
Quick Pause
I recently wrote a book — 101 Things I Never Knew About Python
Check it out here if you wish to support me as a writer!
Link: https://payhip.com/b/vywcf
5) Symmetric difference
Let’s say we have 2 simple sets — a and b
The symmetric difference of 2 sets is another set that contains the elements that appear in either set but not both — essentially elements not in the intersection.
In the case of a and b, the symmetric difference would be {1, 2, 5, 6}, as only these elements don’t appear in both a and b.
To find the symmetric difference of sets a and b, we can use a.symmetric_difference(b). Alternatively, we can use the bitwise XOR operator a ^ b.
6) Combining sets using *
We can combine 2 sets using the * operator.
Since we are combining sets and unpacking singular elements instead of a key-value pair, we use * instead of ** (We use ** when combining dictionaries)
We can also 1) combine 3 or more sets this way 2) combine sets with singular values.
7) Combining sets using .update()
We can also combine 2 sets using the .update() method.
When we do a.update(b), we add everything from b into a. a now has more stuff, but b remains the same as before.
Conversely, when we do b.update(a), we add everything from a into b, while a itself remains unchanged.
8) .isdisjoint() .issubset() & .issuperset()
Here are some useful built-in boolean set methods that check for certain conditions.
2 sets are disjoint if they have no common element. We can check if 2 set (a and b) are disjoint using a.isdisjoint(b)
A set is a subset of another if every element in this set exists in the other set. a is a subset of b if every element in a exists in b.
We can check if a is a subset of b using a.issubset(b)
A superset is the opposite of a subset — A set is a superset of another if it contains every element in the other set.
We can check if a is a superset of b using a.issuperset(b)
Conclusion
Hope you learnt at least a couple new things about sets in Python today.
Cheers,
Liu