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 map()
Python Dictionary setdefault()
Python Program Read a File Line by Line Into a List
Python dir()
Python Deep Learning Cookbook - Indra den Bakker
Python Dictionary get()
Building Chatbots with Python Using Natural Language Processing and Machine Learning - Sumit Raj
Node.js vs Python for Backend Development
Python list()
Python Set discard()
Python String isidentifier()
Python Program to Convert Decimal to Binary, Octal and Hexadecimal
Python Strings
Python Program to Get the File Name From the File Path
Python String format_map()
Python Program to Convert Kilometers to Miles
Python Program to Parse a String to a Float or Int
Deep Learning with Python - A Hands-on Introduction - Nikhil Ketkar
Python List index()
Python Program to Check Armstrong Number
Python Set copy()
Python List
Converting Between an Array and a Set in Java
Python String expandtabs()
Python Set union()
Python Machine Learning Cookbook - Practical solutions from preprocessing to Deep Learning - Chris A...
Python Program to Print the Fibonacci sequence
Python if...else Statement
Python Program to Print all Prime Numbers in an Interval
Python Program to Display Fibonacci Sequence Using Recursion
Python File I/O Operation
Python Program to Get the Last Element of the List