Home » Python » Python programs

# Python | Tuple List Intersection

In this program, we are given two tuples. We need to create a Python program to perform tuple Intersection.

Submitted by Shivang Yadav, on November 10, 2021

Tuples in Python are common types of collections that are used commonly for storing data records in python for processing. In fields like data science and AI, we need to process data tuples to extract fruitful information. In this problem, we will create a python program to perform tuple intersection.

Before going further with the problem, let's recap some basic topics that will help in understanding the solution.

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

Tuples in Python is a collection of items similar to list with the difference that it is ordered and immutable.

**Example:**

tuple = ("python", "includehelp", 43, 54.23)

## Tuple Intersection

The intersection of tuples is finding common elements from both the tuples i.e. the intersection tuple consists of all elements that are common in both the tuples.

In this program, we are given two tuples. We need to create a python program to return a tuple that contains the intersection elements i.e. elements that are common in both tuples.

Input: tuple1 = (4, 1, 7, 9, 3, 5) , tuple2 = (2, 4, 6, 7, 8) Output: (4, 7)

Python provides multiple ways to solve the problem and here we will see some of them working.

**Method 1:**

One method to perform tuple intersection is by performing set intersection operation "**&**" which will find the intersection of sets. And we will be performing interconversion from a tuple to set and set to a tuple using **set()** and **tuple()** methods.

# Program to perform tuple intersection # using & operation, # initialising and Printing tuples myTup1 = (4, 1, 7, 9, 3, 5) myTup2 = (2, 4, 6, 7, 8) print("The elements in tuple 1 : " + str(myTup1)) print("The elements in tuple 2 : " + str(myTup2)) # Performing intersection operation intTuple = tuple(set(myTup1) & set(myTup2)) # Result of intersection operation print("The elements of intersection of their tuples : " + str(intTuple))

**Output:**

The elements in tuple 1 : (4, 1, 7, 9, 3, 5) The elements in tuple 2 : (2, 4, 6, 7, 8) The elements of intersection of their tuples : (4, 7)

**Method 2: **

Another method to solve the problem is by using the **intersection()** method in place of **&** operator on sets. In this program also the interconversion is done using the **set()** method and the **tuple()** method.

# Program to perform tuple intersection # using & operation, # initialising and Printing tuples myTup1 = (4, 1, 7, 9, 3, 5) myTup2 = (2, 4, 6, 7, 8) print("The elements in tuple 1 : " + str(myTup1)) print("The elements in tuple 2 : " + str(myTup2)) # Performing intersection operation intTuple = tuple(set(myTup1).intersection(set(myTup2))) # Result of intersection operation print("The elements of intersection of their tuples : " + str(intTuple))

**Output:**

The elements in tuple 1 : (4, 1, 7, 9, 3, 5) The elements in tuple 2 : (2, 4, 6, 7, 8) The elements of intersection of their tuples : (4, 7)

What's New!

- HTML Multiple-Choice Questions (MCQs)
- Advanced CSS Multiple-Choice Questions (MCQs)
- CSS Multiple-Choice Questions (MCQs)
- MS Word Multiple-Choice Questions (MCQs)
- MIS Executive Interview Questions and Answers
- Go Language Interview Questions and Answers
- PL/SQL Multiple-Choice Questions (MCQs)
- SQL Multiple-Choice Questions (MCQs)
- Software Engineering MCQs
- MIS MCQs

TOP Interview Coding Problems/Challenges!

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions!