×

Python Set intersection() method

Python Set intersection() method

The set.intersection() method in Python returns a new set with elements that are similar between two or more sets.

Syntax

set.intersection(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

This method returns a new set with elements that are common to all sets.

Example 1

# Python program explaining
# the set.intersection() method 
 # initializing set1
 set1 = {"a", "b", "c"}
 print("Set 1:",set1) 
 # initializing set2
 set2 = {"a", "z", "e"}
 print("Set 2:",set2)
 # initializing set3 
 set3 = {"a", "g", "m"}
 print("Set 3:",set3)
 # returning a set that contains similarity between the given three sets.
 Intersection = set1.intersection(set2, set3) 
 # printing the result
 print("Intersection value:",Intersection) 

Output

Set 1: {'b', 'c', 'a'}
Set 2: {'e', 'z', 'a'}
Set 3: {'g', 'm', 'a'}
Intersection value: {'a'}

Example 2

# Python program explaining
# the set.intersection() 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"}
# searching common elements between Set A and Set B
print("Intersection between A and B(A ? B): ",A.intersection(B))
# searching common elements between Set B and Set C 
print("Intersection between B and C(B ? C) : ",B.intersection(C))
# searching common elements between Set C and Set D
print("Intersection between C and D(C ? D) : ",C.intersection(D))
# searching common elements among Set A, Set B, Set C, Set D
print("Intersection between A, B, C and D(A ? B ? C ? D ) : ",A.intersection(B, C, D)) 

Output

Intersection between A and B(A ? B):  {'a'}
Intersection between B and C(B ? C) :  {'a', 'u'}
Intersection between C and D(C ? D) :  {'m'}
Intersection between A, B, C and D(A ? B ? C ? D ) :  set() 

Related Topics

Problem-solving with algorithm and data structures using Python

What is problem-solving? There is no universal method for solving problems. It's frequently a special process that balances your immediate and long-term goals with your available resources. However, several models emphasise...

3 minutes read.

Python String encode() method

Python String encode() method The string.encode() method in Python returns an encoded version of the string. The default encoding is the current default string encoding Syntax String.encode([encoding[,errors]]) Parameter Encoding: This parameter represents a String specifying...

2 minutes read.

Python String rstrip() method

Python String rstrip() method The string.rstrip() method in Python returns a copy of the string with trailing characters removed. Syntax string.rstrip([chars]) Parameter chars:  This argument represents a string specifying the set of characters to be...

1 minute read.

Python locals() function

Python locals() function The locals() function in Python updates and returns a dictionary representing the current local symbol table. Syntax locals() Parameter NA Return This function returns the local symbol table as a dictionary or returns free...

1 minute read.

How to run a Python file in CMD

Introduction Today, let us learn about how to run a Python file using cmd. Cmd is nothing but command prompt. So first let us know how to run a Python code...

4 minutes read.

Commands in Python

In this tutorial, we will see some of the widely used python commands along with their syntaxes and examples. To make Python more user-friendly, developers have provided these commands to...

12 minutes read.

Inheritance in Python

Inheritance in Python Object-Oriented Programming provides reusable patterns to the code for restricting the redundancy in development projects. One of the basic principles of Object-Oriented Programming that helps achieve recyclable code...

8 minutes read.

Python Bubble Sort

Bubble sort is one of the techniques used to sort the elements in lists in a certain order, either in ascending or descending order. It is called Bubble sort because...

4 minutes read.

Python Dictionary pop() method

Python Dictionary pop() method The dictionary.pop() method in removes the specified item and returns an element from a dictionary having the given key. Syntax dictionary.pop(key[, default]) Parameter key – This argument signifies the key which is to...

2 minutes read.

Change Data Type in Python

Python is a dynamic language where it is not always required to consider every variable type. Python supports a wide range of data types, but There are mainly six data...

3 minutes read.

Python String center() method

Python String center() method The center() method will center align the string, using a specified character (space is default) as the fill character. Syntax string.center(width[, fillchar]) Parameter width This parameter represents the length of the returned string. fillchar...

1 minute read.

Decision Tree Classification in Python

In this article, we will learn the implementation of the decision tree in Sklearn, which is nothing but the Scikit Learn library of python. First, we should learn what classification...

12 minutes read.

Simple GUI calculator using PyQt5 in Python

GUI: The user is provided with information using manipulable visual widgets that don't require command-line input. These interface components respond to the user's interactions per the pre-programmed script, assisting each user's...

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

Image to Text in python

Processing out information from an image is a very big task in all the fields of work such as in development and business sector. The process of converting an electronical...

3 minutes read.

File Explorer using Tkinter in Python

File Explorer: Users can control folders and files on a device using an application known as a file manager or file explorer. Customers can access, edit, copy, delete, and start moving files...

3 minutes read.

Adding item to a python dictionary

The dictionary is one of python’s built-in data structures where it stores key-value pairs. A dictionary is a collection of ordered values that can be changeable. We can add, modify,...

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

Python Console

Console in Python is referred as the command line Interpreter- (CLI) and also knows as Shell and it functions as taking input from the human user and interpreting it through...

6 minutes read.

Executing Shell Commands in Python

This tutorial aims to make us understand what a Shell is, what is the importance of a Shell, what are Shell commands in Python, and how can we execute the...

3 minutes read.