| by Arround The Web | No comments

Python set intersection_update() Method

Set Manipulation” in Python can involve determining which elements are common across sets. The common element between multiple different sets is determined using various Python functionalities. One such functionality is the “intersection_update()” method, which is used to modify the original set by keeping only the elements that are common in all the sets passed as arguments.

This write-up explains a detailed guide on Python’s “set.intersection_update()” method with relevant examples and via the below content:

What is the “set.intersection_update()” Method in Python?

In Python, the “set.intersection_update()” method is used to remove the elements that do not exist in all the specified sets.

Note: The “set.intersection()” method returns the new set by removing all the uncommon items, and the “set.intersection_update()” method modifies the original set by removing the uncommon items from the given set.

Syntax

set.intersection_update(set1, set2 ... etc.)

 
Parameters

In the above syntax, the “set1” parameter specifies the set to search for the equal element with all other specified sets such as “set2”, “set3”, etc.

Return Value

The “set.intersection_update(set1, set2 … etc.)” method does not return any value.

Example 1: Determining the Intersection Between Multiple Sets

The below code is used to compute the intersection between sets:

set1 = {1, 3, 5, 7, 9}
set2 = {1, 2, 5, 7, 9, 11}
set3 = {2, 5, 9, 11, 15}
set1.intersection_update(set2, set3)
print('Set Value =', set1)

 
According to the above code, the “set.intersection_update()” method uses the “set1” with dot syntax and takes the “set2” and “set3” as its arguments to determine the set intersection among all the specified sets and updates the value of “set1”.

Output


The “set” intersection has been calculated successfully, and it can be implied that the elements “9” and “5” are present in all the specified sets.

Example 2: Determining the Intersection Between Two Sets

The below code snippet is used to determine the intersection between two sets:

set1 = {1, 3, 5, 7, 9}
set2 = {1, 24, 35, 7, 39, 11}
set1.intersection_update(set2)
print('Set Value =', set1)

 
In the above code, the associated “intersection_update()” method computes the intersection between the given two sets and updates the original set by removing the uncommon values.

Output


The intersection has been calculated successfully.

Conclusion

The “set.intersection_update()” method is utilized in Python to remove/eliminate the uncommon items from all the given sets. This method modifies the original set and does not retrieve any value. This Python guide presented a detailed guide on the “set.intersection_update()” method using appropriate examples.

Share Button

Source: linuxhint.com

Leave a Reply