×

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

Python int()

Python int() class The int() class in Python returns an integer object constructed from a number or string x. Syntax class int(x, base=10) Parameter x: This parameter represents a number or a string that can be converted into...

1 minute read.

Python Online Compiler

Compose, Run and Offer Python code internet involving OneCompiler's Python online compiler free of charge. It's one of the hearty, highlight-rich web-based compilers for python language, supporting both the adaptations, which...

3 minutes read.

tell() function in Python

Introduction The tell() function in Python is used to return the current position of the file read/write pointer within the file. An integer value is returned by this method, and it...

2 minutes read.

Python Control Flow Statements

This article aims to introduce you to what control flow statements are in general and Control Flow Statements in Python programming Language, the Importance of control flow statements and look...

3 minutes read.

How to make API calls in Python

Information is extremely critical these days since it drives applications and organizations. It is subsequently critical to figure out how to get this information to serve your application. In fundamental...

4 minutes read.

Write Text files in Python

Let's discuss writing the text file in detail. Generally, writing text also follows the same procedure as like reading text. It varies only in some specific places. Steps followed for writing...

4 minutes read.

How to implement classifiers in Python

What is classification? Classification is a type of supervised machine learning problem with a categorical target (response) variable. Given the known label in the training data, the classifier approximates a mapping...

4 minutes read.

Python Skyline

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 in a...

3 minutes read.

Python range() function

Python range() function The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number. Syntax range(stop)        or range(start, stop[, step]) Parameter start: It is...

1 minute read.

Python List pop() method

Python List pop() method The list.pop() method removes the item at the specified position in the list, and return it. If no index is specified, this method removes and returns the last item in the list. Syntax list.pop([i]) Parameter i:...

1 minute read.

Python abs() function

Python abs() function The abs() function returns the absolute value of a number. Syntax abs(x) Parameter x: The parameter ‘x’ can be an integer value, a floating point number or a complex number. Return This function returns...

1 minute read.

Exclusive OR in Python

In Python, the exclusive OR (XOR) operator is represented by the caret symbol (^). It compares each bit of the first operand to the corresponding bit of the second operand,...

3 minutes read.

Python SciPy Library

Introduction SciPy, a logical library for Python is an open-source, BSD-authorized library for arithmetic, science, and design. The SciPy library relies upon NumPy, which gives advantageous and quick N-dimensional exhibit control....

6 minutes read.

Python Seaborn

Seaborn is an open-source library in Python that is used for data visualization and plotting graphs. The plots are used for the Visualization of data. It is built on top...

10 minutes read.

How to create a file in Python?

How to create a file in python? Files provide us a convenient way of storing our information. Once we have our data, the next task is to store it so that...

3 minutes read.

Python Image Processing

What is an Image? Images are the pictures that will define the world, and it has their own story, and consists of information about them and these are useful in many...

9 minutes read.

Python max() function

Python max() function The max() function in Python returns the largest item in an iterable or the largest of two or more arguments. Syntax max(iterable, *[, key, default])               or max(arg1, arg2, *args[, key])   Parameter arg1, arg2, *args: This...

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

Scraping data in python

Data scraping is a technique in which one program extracts a set of data from the output of another program. Web scraping is the most common application of this technique....

6 minutes read.

Python Validator

Validator  The validator is a library available in the python programming language. The library in python consists of all the related modules which can be imported to perform the required operation....

3 minutes read.