Python Set intersection_update() Method
In this tutorial, we will understand about the python set intersection_update() method and its uses.
Python Set intersection_update() Method
The Python set intersection_update() method modifies the original set by keeping only elements found in both sets. Unlike intersection(), this method updates the set in place and returns None. It can also be written using the &= operator.
The syntax of the intersection_update() method is:
1
2
3
set.intersection_update(set2)
# or
set1 &= set2
Python set intersection_update() Parameters
The intersection_update() method takes one parameter:
- set2: Another set or iterable whose common elements will be retained in the original set.
Let’s explore some examples:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# Example 1: Basic intersection_update
set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7, 8}
set1.intersection_update(set2)
print(set1) # Output: {4, 5}
# Example 2: Using operator syntax
numbers1 = {1, 2, 3, 4}
numbers2 = {3, 4, 5, 6}
numbers1 &= numbers2
print(numbers1) # Output: {3, 4}
# Example 3: Multiple set intersection_update
set1 = {1, 2, 3, 4}
set2 = {2, 3, 4, 5}
set3 = {3, 4, 5, 6}
set1.intersection_update(set2, set3)
print(set1) # Output: {3, 4}
This method is memory-efficient when working with large sets.
This post is licensed under CC BY 4.0 by the author.