Home »
Python »
Python programs
Python program to perform cross pairing in tuple list
Here, we are given a list of tuples and we need to perform a cross pairing operation on both the tuple list and return the cross paired tuple.
Submitted by Shivang Yadav, on September 02, 2021
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)
List is a sequence data type. It is mutable as its values in the list can be modified. It is a collection of an ordered set of values enclosed in square brackets [].
Example:
list = [3 ,1, 5, 7]
We have two lists with each element a tuple and we need to perform cross pairing i.e. we need to create a tuple with second elements of tuples whose first elements are the same.
Here is an example for a better understanding of the problem,
Input:
tupList1 = [(1, 8), (9, 6), (0, 8), (4,5)]
tupList2 = [(7, 8), (2, 5), (3, 6), (4,7)]
Output:
(5, 7)
Explanation: Here, only the tuples (4,5) and (4,7) have common first elements. So, cross pairing will consider both of them only.
Solution:
To solve this problem, we will be traversing the list and then checking for each tuple if there exists a tuple in another list that has the same first elements as this tuple. If yes, we will make a tuple with the second values of both the tuples and store them in a list.
For performing this task in the python programming language, we have more than one way as python has many built-in methods to reduce our efforts in tasks.
Method 1:
One way to solve the problem is by using list comprehension along with comparison using condition statements to create a new tuple that will have a second element of the tuple whose first element is the same.
# Python program to perform
# Cross Pairing in Tuple List
# Creating and printing tuple list
myList1 = [(1, 8), (9, 6), (0, 8), (4,5)]
myList2 = [(1, 3), (2, 1), (9, 7), (2, 17)]
print("Tuples of list 1 are " + str(myList1))
print("Tuples of list 2 are " + str(myList2))
# Performing cross Pairing in Tuple list
crossTupleList = [(sub1[1], sub2[1]) for sub2 in myList2 for sub1 in myList1 if sub1[0] == sub2[0]]
# Printing crossed Tuple list
print("Resultant Tuple list : " + str(crossTupleList))
Output:
Tuples of list 1 are [(1, 8), (9, 6), (0, 8), (4, 5)]
Tuples of list 2 are [(1, 3), (2, 1), (9, 7), (2, 17)]
Resultant Tuple list : [(8, 3), (6, 7)]
Method 2:
An alternate method to solve the problem is by using the zip() method with the condition of the first element's equality along with list comprehension.
# Python program to perform
# Cross Pairing in Tuple List
# Creating and printing tuple list
myList1 = [(1, 8), (9, 6), (0, 8), (4,5)]
myList2 = [(1, 3), (2, 1), (9, 7), (2, 17)]
print("Tuples of list 1 are " + str(myList1))
print("Tuples of list 2 are " + str(myList2))
# Performing cross Pairing in Tuple list
crossTupleList = [(val1[1], val2[1]) for val1, val2 in zip(myList1, myList2) if val1[0] == val2[0]]
# Printing crossed Tuple list
print("Resultant Tuple list : " + str(crossTupleList))
Output:
Tuples of list 1 are [(1, 8), (9, 6), (0, 8), (4, 5)]
Tuples of list 2 are [(1, 3), (2, 1), (9, 7), (2, 17)]
Resultant Tuple list : [(8, 3)]
Python Tuple Programs »