×

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

Difference between Input() and raw_input() functions in Python

There are two input functions in Python that are used for taking input are given below: raw_input()input() What is raw_input() Function? raw_input() function is a built-in function in Python. It is used to...

6 minutes read.

Python Variables

Variables are one of the most important terms we should be familiar with if we want to be good programmers. In simpler words, we use variables to store a value....

4 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: SetBitmap() function in wxPython

SetBitmap() function In the last tutorial, we have discussed about the GetLabelText() function of wx.MenuItem class which is one of the important function of this class. Now, in this tutorial, we...

6 minutes read.

How to run Python program in CMD

How to run python program in cmd Command Prompt provides us all together with a different approach for dealing with our programs. The programs can be executed by accessing the directories. In...

3 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.

Python String isspace() method

Python String isspace() method The string.isspace() method returns a Boolean value true if there are only whitespace characters in the given string. This function is used to check if the given...

2 minutes read.

Python Infinity

Introduction We will discover how to define an infinite number in Python in this tutorial. Infinity, as we all know, is a value that cannot be defined and can either be...

5 minutes read.

Pointers in Python

In this tutorial, we will study what pointers are and if they have any utility in python. Now, let us understand what pointers are Pointers Pointers are special variables used to store the...

3 minutes read.

Python frozenset()

Python frozenset() class The frozenset() class in Python returns a new frozenset object, optionally with elements taken from iterable. Syntax class frozenset([iterable]) Parameter iterable: This parameter represents an iterable object, like list, set, tuple etc. Return This class returns an unchangeable...

1 minute read.

Artificial intelligence mini projects ideas in python

Artificial intelligence "The human race started from the invention of the wheel", and today we are about to advance to the Industrial level 4.0, where machines can work, talk, think and...

6 minutes read.

Python Comments

In this tutorial, we will discuss the importance of comments in the Python code and how various types of comments can be inserted in the code. Comments are used to describe...

3 minutes read.

Screen Rotation app 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...

3 minutes read.

How to Install Scikit-Learn

Sklearn or Scikit-learn is a python library used for machine learning. It contains many features like classification, regression, clustering, and Dimensionality reduction algorithms. Sklearn is used to build machine learning...

3 minutes read.

Python String startswith() method

Python String startswith() method The string.startswith() method in Python returns a boolean value ‘True’ if the given string starts with the prefix, otherwise it returns False. Syntax startswith(prefix[, start[, end]]) Parameter prefix: This parameter signifies the value to check. start(optional):...

1 minute read.

Python History

Python is one of the top trending, widely-used programming languages. Python is a general-purpose programming language. Python language is known for its features. Some features are given below: SimplisticFree and open...

4 minutes read.

Python set()

Python set() Class The set() class in Python returns a new set object, optionally with elements taken from iterable.  Syntax class set([iterable]) Parameter iterable : This parameter represents a sequence, collection or an iterator object Return This function returns a...

1 minute read.

Continue and Pass Statements in Python

Difference between continue and pass statements in Python The tasks can be repeated and automated efficiently using loops in Python. Sometimes we have to exit from the entire loop completely, we...

2 minutes read.

Difference between python list and tuple

In this tutorial, we will understand the key differences between python lists and tuples in python. Firstly, let us see the similarities between Lists and Tuples. Both are two data types...

4 minutes read.

Python Logistic Regression with Sklearn & Scikit

In this article, we'll walk through a tutorial for utilising the Python Sklearn (formerly known as Scikit Learn) package to implement logistic regression. To assist you in remembering the concept,...

18 minutes read.