Table of Contents
The intersection_update() updates the set calling intersection_update() method with the intersection of sets.
The intersection of two or more sets is the set of elements which are common to all sets.
To learn more, visit Python set Intersection.
The syntax of intersection_update() is:
A.intersection_update(*other_sets)
1. intersection_update() Parameters
The intersection_update() method allows an arbitrary number of arguments (sets).
Note: * is not a part of the syntax. It is used to indicate that the method allows an arbitrary number of arguments.
2. Return Value from Intersection_update()
This method returns None (meaning it does not have a return value). It only updates the set calling the intersection_update() method.
For example:
result = A.intersection_update(B, C)
When you run the code,
- result will be
None - A will be equal to the intersection of A, B, and C
- B remains unchanged
- C remains unchanged
3. Example 1: How intersection_update() Works?
A = {1, 2, 3, 4}
B = {2, 3, 4, 5}
result = A.intersection_update(B)
print('result =', result)
print('A =', A)
print('B =', B)
Output
result = None
A = {2, 3, 4}
B = {2, 3, 4, 5}
4. Example 2: intersection_update() with Two Parameters
A = {1, 2, 3, 4}
B = {2, 3, 4, 5, 6}
C = {4, 5, 6, 9, 10}
result = C.intersection_update(B, A)
print('result =', result)
print('C =', C)
print('B =', B)
print('A =', A)
Output
result = None
C = {4}
B = {2, 3, 4, 5, 6}
A = {1, 2, 3, 4}
Related posts:
Python Program to Display Fibonacci Sequence Using Recursion
Python bytearray()
Python int()
Python Machine Learning - Sebastian Raschka
Python Dictionary clear()
Python Program to Multiply Two Matrices
Python Program to Count the Number of Digits Present In a Number
Python Set discard()
How to get current date and time in Python?
Python String splitlines()
Python Program to Differentiate Between del, remove, and pop on a List
Python Generators
Python Program to Add Two Numbers
Machine Learning Mastery with Python - Understand your data, create accurate models and work project...
Python Deeper Insights into Machine Learning - Sebastian Raschka & David Julian & John Hearty
Python Program to Safely Create a Nested Directory
Python Get Current time
Natural Language Processing with Python - Steven Bird & Ewan Klein & Edward Loper
Machine Learning Applications Using Python - Cases studies form Healthcare, Retail, and Finance - Pu...
Python Program to Get a Substring of a String
Python Tuple index()
Python dict()
Python Program to Find the Factorial of a Number
Python String isidentifier()
Python format()
Building Machine Learning Systems with Python - Willi Richert & Luis Pedro Coelho
Python Program to Create a Long Multiline String
Python Operators
Python Set union()
Python String join()
Python String title()
Python Program to Access Index of a List Using for Loop