×

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

How to Iterate through a Dictionary in Python

In this tutorial, we will learn how to interate throught the dictionary in the Python programming, using different approaches with example. Introduction to Dictionary in Python Dictionary is a special type of...

4 minutes read.

Imread Python

In this walkthrough, we'll go over the specifics of using the imread() method of OpenCV-Python and various methods for loading images. Imread is an Image reading library. One of the most helpful...

7 minutes read.

Python Slice from Last Occurrence of K

Introduction We already know that in Python, cutting produces a sub-string out of a string. The variables start, stop, and step are used to set the slicing range. When dealing on...

3 minutes read.

Class and Static Methods in Python

The method is an important concept to learn for every programmer or data analyst. We know that in OOP's concept, each object has its attributes and behaviors defined through methods....

3 minutes read.

How To Compare Two Strings In Python

In this article, we will discuss how to compare two strings in Python. So, before that, let’s have a quick revision on “What are strings?” Strings are a sequence of characters that...

5 minutes read.

How to comment out a block of code in Python

When you are writing code in Python, you may want to document it. You need to mention why a piece of code functions, for instance. Mathematics, algorithms, and intricate business...

4 minutes read.

Checking whether a String Contains a Set of Characters in python

In this tutorial, we will learn how to examine or check whether a string contains any set of characters or a substring and if it contains, we will learn how...

6 minutes read.

Python Typing Module

An Introduction to the Typing Module The typing module is introduced in Python version 3.5 and used to provide hinting method types in order to support static type checkers and linters...

10 minutes read.

Python K-Means Clustering

K-Means Clustering is a basic yet incredible calculation in information scienceThere are a plenty of true utilizations of K-Means clustering (a couple of which we will cover here)This far reaching...

25 minutes read.

Python String splitlines() method

Python String splitlines() method The string.splitlines() method in Python splits the specified string and returns a list of the lines in the string, breaking at line boundaries.  Syntax splitlines([keepends]) Parameter keepends(optional): This parameter specifies if...

1 minute read.

Python PPTX

Python-pptx is a python library that enables the user to create and update PowerPoint (.pptx) presentations. It is used to create modified PowerPoint presentations from the db content that can...

10 minutes read.

Find Median of List in Python

The median is an enlightening measurement that is utilized as a proportion of the focal inclination of a circulation. It is equivalent to the centre worth of the conveyance. There...

3 minutes read.

Add Dictionary to Dictionary in Python

Dictionary in python: Python's execution of a data model, known more commonly as an implicit array, is a dictionary. A dictionary is made up of a group of key-value pairs. Each...

4 minutes read.

Cube Root in Python

In general, the cube is a three-dimensional solid figure that has 6 square faces. It is also called a geometrical shape with six equal faces, eight vertices, and twelve edges....

3 minutes read.

Write Dictionary to CSV 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...

4 minutes read.

Python String find() method

Python String find() method The string.find() method in Python finds the first occurrence of the specified value and returns the lowest index in the string if the substring sub is found else it...

2 minutes read.

Reverse a String in Python

Python is an object-oriented high-level programming language. Python has dynamic semantics and has high-level built-in data structures which support dynamic typing and dynamic binding. Python provides rapid development. It has...

4 minutes read.

Python format() function

Python format() function The format() function in Python formats a specified value into the given format. A ‘TypeErrorexception’ is raised if the method search reaches the object and the format_spec is non-empty, or if either the format_spec or the...

1 minute read.

GUI to extract lyrics from a song Using Tkinter in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

4 minutes read.

Hypothesis Testing in Python

Hypothesis Testing in python is widely used along with statistics. Many libraries in Python are very useful for statistics and machine learning. Libraries like numpy, scripy etc. help in hypothesis testing in...

12 minutes read.