×

Python Set intersection_update() method

Python Set intersection_update() method

The set.intersection_update() method in Python removes the items that is not present in both sets.

It is different from the set.intersection() method, because the intersection()method returns a new set, with only  the common elements whereas the intersection_update() method removes the unwanted items from the original set.

Syntax

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

Parameter

set1- This parameter represents the set to search for equal items

set2(optional)- This argument signifies the other set to search for equal items. One can compare as many sets as needed, only need to separate the sets with a comma.

Return

None

Example 1

# Python program explaining
# the intersection_update() method
#initializing set1
set1 = {"mango", "banana", "apple","tomato"}
print("Set 1:",set1) 
# initializing set2
set2 = {"potato", "spinach","Carrot", "apple"}
print("Set 2:",set2)
# Removing the items that is not present in both sets.
set1.intersection_update(set2)  
print("After intersection_update() method...")
print("Updated Set 1:",set1) 

Output

 Set 1: {'tomato', 'apple', 'mango', 'banana'}
 Set 2: {'apple', 'Carrot', 'potato', 'spinach'}
 After intersection_update() method...
 Updated Set 1: {'apple'} 

Example 2

# Python program explaining
 # the intersection_update() method
 # initializing set1
 set1 = {"a", "b", "c","q","R"}
 print("Set 1:",set1) 
 # initializing set2
 set2 = {"a", "z", "e","q","R"}
 print("Set 2:",set2)
 # initializing set3 
 set3 = {"a", "g", "m","q","R"}
 print("Set 3:",set3)
 # removing the items that is not present in three of the sets. .
 set1.intersection_update(set2, set3) 
 # printing the result
 print("Updated Set 1:",set1) 

Output

 Set 1: {'R', 'a', 'b', 'q', 'c'}
 Set 2: {'R', 'z', 'a', 'q', 'e'}
 Set 3: {'R', 'a', 'm', 'q', 'g'}
 Updated Set 1: {'R', 'a', 'q'} 

Example 3

# Python program explaining
 # the intersection_update() method
 # initializing first set
 A = {"a", "e", "i"}
 # initializing second set 
 B = {"o", "u","a"}
 # initializing third set
 C = {"u", "z", "a","m"}
 # initializing fourth set
 D = {"f","h","l","h","m"} 
 # deleting the element that is not common between Set A and Set B
 A.intersection_update(B,C,D)
 # when no element is common
 print("After the intersection_update() method...")
 # printing set A 
 print("set A: ",A)
 # printing set B
 print("set B: ",B)
 # printing set C
 print("set C: ",C) 
 # printing set D
 print("set D: ",D) 

Output

 After the intersection_update() method...
 set A:  set()
 set B:  {'o', 'a', 'u'}
 set C:  {'a', 'm', 'z', 'u'}
 set D:  {'l', 'm', 'h', 'f'} 

Related Topics

Python Image Processing

What is an Image? Images are the pictures that will define the world, and it has their own story, and consists of information about them and these are useful in many...

9 minutes read.

Python Random Module

The tutorial for the Python random module demonstrates how to produce pseudo-random integers in Python. Random Number Generator (RNG) The RNG (random number generator) generates a series of values with no discernible...

8 minutes read.

N2 in Python

N2 is known as Nearest Neighbor Algorithm, because it contains 2 N's (N-Nearest, N-Neighbor). This is a library in the python build using C++ and Python. Before N2 was made,...

3 minutes read.

Enumerate() Function in Python

The enumerate() function in Python is used to convert a data collection object into an enumerate object. It returns an object that contains a counter as a key for each...

3 minutes read.

Python - Binomial Distribution

Introduction Definition of the Binomial Distribution The method of counting how many instances of a specific event there have been is called the binomial distribution. It will outline the possible outcomes or...

4 minutes read.

What does the if __name__ == "__main__" do in Python

In this article, you will learn about the If__name__==__main__ is a statement in python to define modules and the names of the modules. This statement plays a vital role in...

3 minutes read.

Returning Multiple Values in Python

Python is considered a general-purpose programming language; it is a high-level programming language that is not much difficult and easier to learn. It is rich in libraries that can be...

3 minutes read.

Python String isalnum() method

Python String isalnum() method The string.isalnum () method in Python returns a boolean value true if all characters in the string are alphanumeric else for any other value it returns false. Syntax String.isalnum() Parameter NA Return This...

1 minute read.

How to Configure Python Interpreter in Eclipse

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Map Syntax in Python

Introduction: In Python, a function called map acts as an iterator, returning a result after each item in an iterable has been subjected to a function (tuple, lists, etc.). When you...

6 minutes read.

Working with CSV files in Python

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

7 minutes read.

Exit program in python

Exit program in python The quit(), exit(), and sys.exit() functions have almost the same functionality as they raise the SystemExit exception by which the Python interpreter exit and no stack traceback...

2 minutes read.

Python setattr() function

Python setattr() function The setattr() function sets the value of the specified attribute of the specified object. Syntax setattr(object, name, value) Parameter object: This parameter represents any object. name: This parameter represents the name of the attribute you...

1 minute read.

Comment starts with the symbol in Python

Python programming language: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a...

3 minutes read.

Text detection using Tkinter in Python

Tkinter: The standard Python technique for building Graphical User Interfaces (GUIs) is Tkinter, which is included in all popular Python distributions. The only framework included in the Python standard library is...

4 minutes read.

Python Letter to Number

Python Letter to Number In this tutorial, we will convert the given letters into numbers using a Python. We will convert the given letter into the letter value as defined in...

3 minutes read.

Python program to find the GCD or HCF

Python program to find the GCD or HCF The largest positive integer that perfectly divides the two numbers is the HCF (Highest Common Factor) or GCD (Greatest Common Deviser). For example,...

5 minutes read.

Python Indentation

Overview The spaces at the start of a code line are known as an indentation in Python programming. Indentation is only used for readability in other programming languages like C, C++,...

8 minutes read.

PyDev with Python IDE

What is IDE? Integrated Development Environment is the abbreviation of IDE. It is a coding tool that automates code editing, finding errors and testing. IDE combines all the developer tools into...

3 minutes read.

Python String split() method

The string.split() method in Python splits a string into a list and returns a list of the words in the string. If the parameter maxsplit is given, at most maxsplit splits are done. If maxsplit is...

2 minutes read.