×

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

Best Database for Python

Database The collection of structured data or information in an organized format in a computer system is known as Database. The data is inserted, deleted, updated, controlled, or manipulated in a...

6 minutes read.

Python JSON

In this tutorial, we will learn about JSON in the Python programming language. We will focus on its features, Guidelines to be kept in mind while writing its syntax, the...

3 minutes read.

How to Convert A List into String In Python?

How to Convert A List into String In Python? List is a data structure in Python that can hold values of different data types. The values are enclosed in square brackets...

3 minutes read.

Arithmetic Expressions in Python

What is Python Expression? Expressions are collections of operands and operators. Python expressions are translated by the Python interpreter into some value or outcome. In Python, an expression is made up...

13 minutes read.

Python Classification

Identification and grouping of items or concepts into specified categories this process is known the classification. Data can be separated and sorted in data management according to predetermined criteria for...

6 minutes read.

Python String index() method

Python String index() method The string.index() method in Python finds the lower index of the first occurrence of the specified value or raise a ValueError if the substring is not found. Syntax index(sub[, start[, end]]) Parameter sub:...

2 minutes read.

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.

Augmented reality in python

In this article, we shall learn about augmented reality in python. Augmented reality is a technology that captures the world around you and adds virtual content or objects on top of...

3 minutes read.

Python Dictionary values() method

Python Dictionary values() method The dictionary.values() method in Python returns a view object that displays a list of all the values in the specified dictionary. Syntax dictionary.values() Parameter NA Return This method returns a view object. The...

1 minute 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.

Python Word Tokenizer

Python Overview Python is an Object-Oriented high-level language. Python is designed to be highly beginner-friendly. Python has an English-like syntax, which is very easy to read. Python is an interpreted language...

4 minutes read.

Python SQLite

SQLite It is an RDBMS (Relational Database Management System). It is an embedded, serverless, transactional SQL database engine. It is an open-source application. It is named SQLite because of its lightweight....

3 minutes read.

Python Filter List

To filter a list, we use filter () method function. The filter () will test every element true or not in the sequence. Syntax: Filter(function, sequence) Parameters: Function: Function that check and verifies if...

2 minutes read.

How to Sort a String in Python?

The characters in the string are sorted or put in alphabetical order using the sort string function in Python. Python has built-in techniques for sorting strings available. Since we occasionally...

6 minutes read.

Character Set in Python

A collection of legal characters that will recognize by a programming language known as a Character set. Taking python programming language as an instance. The set of characters supported by the...

6 minutes read.

How to write a program in python?

How to write a program in python? In python, writing programs is not a big deal. What we need to work on is our logic and some basic rules of this...

4 minutes read.

How to Reverse a number in Python?

In Python, conditional statements can be combined with the list() method, slice() method, recursion() method, and other predefined techniques to generate reverse logical functions. The reverse() method is the most...

7 minutes read.

Python String count() method

Python String count() method The string.count() method returns the number of times a specified value appears in the string. Syntax string.count(sub[, start[, end]]) Parameter sub: This parameter represents a substring to be searched. start: This parameter...

1 minute read.

Subprocess in Python

In this tutorial, we will understand what is a subprocess in python and will understand how to use it. Subprocess A subprocess is a very prevailing portion of the python library. It...

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.