A set is an unordered collection of unique elements. Python provides several built-in methods for manipulating sets, including the intersection_update()
method. The intersection_update()
method updates the set by keeping only the elements that are common to the set and the given sets. In other words, it updates the set to contain only the intersection of the set and the given sets.
Syntax
set.intersection_update(set1, set2, set3, ...)
The intersection_update()
method takes one or more sets as its arguments and updates the set to contain only the elements that are common to all the sets. The sets can be passed as separate arguments or as elements of an iterable (e.g., a list, tuple, or another set).
If no arguments are passed to the intersection_update()
method, it updates the set to contain no elements, i.e., it empties the set.
The intersection_update()
method modifies the set in place and does not return anything. If you want to create a new set containing the intersection of the sets, you can use the intersection()
method.
Examples
Example 1: Using intersection_update() with two sets
Let’s create two sets and update one of them to contain only the common elements using the intersection_update()
method:
# Create two sets set1 = {1, 2, 3, 4, 5} set2 = {4, 5, 6, 7, 8} # Update set1 to contain only the common elements set1.intersection_update(set2) # Print the updated set1 print(set1)
Output:
{4, 5}
In the above example, we created two sets (set1
and set2
) and updated set1
to contain only the common elements using the intersection_update()
method. The updated set1 contains only the common elements of the two sets (4 and 5).
Example 2: Using intersection_update() with three sets
Let’s create three sets and update one of them to contain only the common elements using the intersection_update()
method:
# Create three sets set1 = {1, 2, 3, 4, 5} set2 = {4, 5, 6, 7, 8} set3 = {5, 6, 7, 8, 9} # Update set1 to contain only the common elements set1.intersection_update(set2, set3) # Print the updated set1 print(set1)
Output:
{5}
In the above example, we created three sets (set1
, set2
, and set3
) and updated set1
to contain only the common elements using the intersection_update()
method. The updated set1 contains only the common element of all three sets (5).
Example 3: Using intersection_update() with an empty set
Let’s create an empty set and update it using the intersection_update()
method:
# Create an empty set set1 = set() set2 = {1, 2, 3, 4, 5} # Update set1 to contain only the common elements set1.intersection_update(set2) # Print the updated set1 print(set1)
Output:
set()
In the above example, we created an empty set (set1
) and updated it to contain only the common elements with another set (set2
) using the intersection_update()
method. Since the empty set has no elements, the updated set1 is also empty.
Use Cases
The intersection_update()
method can be useful in many situations where you need to find the common elements between two or more sets and update one of them to contain only the common elements. For example, you might use the intersection_update()
method to:
- Filter out non-matching elements from a set of records or items.
- Find the common elements between multiple sets of data.
- Remove duplicates from a set by keeping only the unique elements.
Overall, the intersection_update()
method is a useful tool for manipulating sets and can help you perform complex operations on sets in a few lines of code.