Python’s set.intersection_update(sets) removes each element that’s not a member of all arguments sets.
Here’s a minimal example that modifies existing set object s to be the intersection between sets s and t:
>>> s = {1, 2, 3, 4}
>>> t = {3, 4, 5}
>>> s.intersection_update(t)
>>> s
{3, 4}
Syntax
Let’s dive into the formal syntax of the set.intersection_update() method.
set.intersection_update(*sets)
Argument
Data Type
Explanation
*sets
One or more sets
The elements of those sets will be removed from the original set on which the method is called.
Return Value of set.intersection_update()
The return value of set.intersetion_update() is None. But the method has side effects—it modifies the original set on which it is called.
Advanced Examples set.intersection_update()
There are some subtleties you need to understand regarding the set intersection update method. Let’s dive into them by example!
The straightforward example is to calculate the intersection of a set with one of its subsets. In this case, the result is the subset because all elements in the subset are already elements of the superset, by definition.
>>> s = {1, 2, 3}
>>> s.intersection_update({1, 2})
>>> s
{1, 2}
But what if you’d invert this and calculate the intersection of a subset and a superset? In this case, the result is the same as before:
>>> s = {1, 2}
>>> s.intersection_update({1, 2, 3})
>>> s
{1, 2}
Can you compute the intersection of a set and an empty set? Sure! The result is the empty set.
>>> s = {1, 2, 3}
>>> s.intersection_update(set())
>>> s
set()
What if there’s an overlap between both sets but both sets have elements that are not contained in the other one? In this case, you’d take only the elements in the overlap.
>>> s = {1, 2, 3}
>>> s.intersection_update({2, 3, 4})
>>> s
{2, 3}
Set Intersection Multiple Set Arguments
You can compute the intersection of an original set and an arbitrary number of set arguments. In this case, it will overwrite the original set on which it is called as the set that contains only elements that are members of all involved sets.
Only the element 1 is a member of all involved sets.
Set intersection() vs intersection_update()
The set.intersection() method returns a new set whereas the set.intersection_update() operates on the set it is called upon and returns None.
s.intersection(t) – Creates a new set with the intersection of s and t. The original set s remains unchanged. Returns the new set.
s.intersection_update(t) – Works on the original set s and removes all elements that are not in t. Returns None.
Here’s an example that shows the difference between both methods:
>>> s = {1, 2, 3}
>>> t = s.intersection({1, 2})
>>> s
{1, 2, 3}
And the set.intersection_update() updates on an existing set s and returns None:
>>> s = {1, 2, 3}
>>> s.intersection_update({1, 2})
>>> s
{1, 2}
What is the Time Complexity of set.intersection_update() in Python?
The runtime complexity of the set.intersection_update() method on a set with n elements and a set argument with m elements is O(min(n, m)) because you need to check for the smaller set whether each of its elements is a member of the larger set. Then, you need to remove the element that is not a member of the other set. Checking membership and element removal is both O(1), so the runtime complexity is O(min(n, m)) * O(1) * O(1) = O(min(n, m)).
You can see this in the following simple experiment where we run the set method multiple times for increasing set sizes:
Figure: Runtime increases linearly with increasing set sizes.
I ran this experiment on my Acer Aspire 5 notebook (I know) with Intel Core i7 (8th Gen) processor and 16GB of memory. Here’s the code of the experiment:
import matplotlib.pyplot as plt
import time
sizes = [i * 10**5 for i in range(50)]
runtimes = []
for size in sizes:
s = set(range(size))
t = set(range(0, size, 2))
# Start track time ...
t1 = time.time()
s.intersection_update(t)
t2 = time.time()
# ... end track time
runtimes.append(t2-t1)
plt.plot(sizes, runtimes)
plt.ylabel('Runtime (s)')
plt.xlabel('Set Size')
plt.show()
Other Python Set Methods
All set methods are called on a given set. For example, if you created a set s = {1, 2, 3}, you’d call s.clear() to remove all elements of the set. We use the term “this set” to refer to the set on which the method is executed.
Create and return a new set containing all elements of this set except the ones in the given set arguments. The resulting set has at most as many elements as any other.
Replace this set with the symmetric difference, i.e., elements in either this set or the specified set argument, but not elements that are members of both.
Update this set with all elements that are in this set, or in any of the specified set arguments. The resulting set has at least as many elements as any other.