×

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

Create the First GUI Application using PyQt5 in Python

GUI: A graphical user interface, or GUI, is present on most personal computers. It provides a simple experience for individuals with various computing skill levels. GUI apps may take more resources...

3 minutes read.

Python String rjust() method

Python String rjust() method The string.rjust() method in Python returns a right-justified string of a given minimum width where the padding is done using the specified fillchar (default is a space). It returns...

2 minutes read.

Python Kwargs Example

In this article, we'll talk about Python's kwargs notion. In Python, kwargs has two stars and passes a variable number of keyworded argument lists to the function, whereas args has...

5 minutes read.

How to convert Float to Int in Python?

A float value can be converted to an int via type conversion, an explicit method of transforming an operand to a certain type. But it must be noted that such...

3 minutes read.

EDA in Python

The EDA is the exploratory data analysis; the data scientists mainly use the EDA to understand the main features of the data quickly, the variables in python and the relationship...

6 minutes read.

How to run a Program in Python

How to run a Program in Python Writing a program in Python is an easy task, beginners who are ready to kickstart their career in the world of programming can create...

3 minutes read.

Python pycountry

What is Pycountry? 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...

4 minutes read.

Python md5_file() function

Python md5_file() function The md5_file() function in PHP calculates the md5 hash of a given file. Syntax md5_file ( string $filename [, bool $raw_output ] )  Parameter filename(required)- This parameter signifies the file to be calculated. raw_output(optional)- It takes a boolean value that specifies hex or binary...

1 minute read.

How to Take Input in Python

How To Take Input In Python Python has various in-built functions that help in code redundancy. Let's discuss the usage of some functions- ascii() – it returns a readable representation of objects.format()...

4 minutes read.

Python Set update() method

Python Set update() method The set.update() method in Python updates the current set, by adding items from another set. If an element is common in both the sets, only one appearance of this item...

1 minute read.

Function annotations() in Python

What is Function Annotations? Function annotations provide a way related to different parts of a function at compile time with arbitrary Python expressions. It doesn’t have life in Python runtime execution....

3 minutes read.

Classes & Objects in Python

Classes and Objects are used in most modern programming languages, mainly in Object-oriented Programming languages. What is meant by Object Oriented Programming language? Usage of objects and their components in order to...

7 minutes read.

Python Generator

Python Generator: A Function is said to be a Python Generator that produces or generates a sequence of results. A Python Generator maintains its native state to work so that...

6 minutes read.

Python Set clear() Method

Python Set clear() Method The set.clear() method removes all the elements from the set. Syntax set.clear() Parameter NA Return None Example 1 # Python program explaining # the set.clear() method # initializing the set fruits = {"watermelon", "banana", "apple"} # printing the set...

2 minutes read.

Python Program to Print Sum of all Elements in an Array

Python program to print sum of all elements in an array A set of objects stored in contiguous memory locations is referred to as an array. The concept is to keep...

2 minutes read.

Convert XML to JSON in Python

XML conversion is very useful if we work on an API that returns data in JSON format and the source of data is in XML format. JSON A JSON file reserves the...

4 minutes read.

Pltpcolor in Python

Python: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It is said...

3 minutes read.

Python String ljust() method

Python String ljust() method The string.ljust() method in Python will left align the string, where the padding is done using the parameter fillchar (space is default). Syntax String.ljust(width[, fillchar]) Parameter width: This parameter represents the...

2 minutes read.

Python Set remove() method

Python Set remove() method The set.remove() method in Python removes the specified element from the set if it is present in the set else this method will raise an error if the specified item does...

2 minutes read.

Python bytearray()

Python bytearray() Class The bytearray() class is used to return a bytearray object which is an array of the specified bytes. It gives a mutable sequence of integers in the range 0...

1 minute read.