Python | Tuple list cross multiplication
Last Updated :
09 Apr, 2023
Sometimes, while working with Python records, we can have a problem in which we need to perform cross multiplication of list of tuples. This kind of application is popular in web development domain. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using list comprehension + zip() The combination of the above functionalities can be used to perform this particular task. In this, we iterate through the list using list comprehension and the multiplication across lists is performed with the help of zip().
Python3
# Python3 code to demonstrate working of
# Tuple list cross multiplication
# using list comprehension + zip()
# initialize lists
test_list1 = [(2, 4), (6, 7), (5, 1)]
test_list2 = [(5, 4), (8, 10), (8, 14)]
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
# Tuple list cross multiplication
# using list comprehension + zip()
res = [(x[0] * y[0], x[1] * y[1]) for x, y in zip(test_list1, test_list2)]
# printing result
print("The multiplication across lists is : " + str(res))
Output : The original list 1 : [(2, 4), (6, 7), (5, 1)]
The original list 2 : [(5, 4), (8, 10), (8, 14)]
The multiplication across lists is : [(10, 16), (48, 70), (40, 14)]
Time complexity: O(n), where n is the length of the lists test_list1 and test_list2.
Auxiliary space: O(n), where n is the length of the lists test_list1 and test_list2, since the result is stored in a new list.
Method #2 : Using loop + zip() + map() This is yet another way to perform this task. This is similar to above method, the difference is that multiplication is performed by explicit function and extending logic to each element is done by map().
Python3
# Python3 code to demonstrate working of
# Tuple list cross multiplication
# using max() + zip() + loop
# getting Product
def prod(val) :
res = 1
for ele in val:
res *= ele
return res
# initialize lists
test_list1 = [(2, 4), (6, 7), (5, 1)]
test_list2 = [(5, 4), (8, 10), (8, 14)]
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
# Tuple list cross multiplication
# using max() + zip() + loop
res = [tuple(map(prod, zip(a, b))) for a, b in zip(test_list1, test_list2)]
# printing result
print("The multiplication across lists is : " + str(res))
Output : The original list 1 : [(2, 4), (6, 7), (5, 1)]
The original list 2 : [(5, 4), (8, 10), (8, 14)]
The multiplication across lists is : [(10, 16), (48, 70), (40, 14)]
The time complexity of this code is O(nm), where n is the length of test_list1 and m is the length of test_list2.
The space complexity of this code is O(n), where n is the length of test_list1.
Method #3 : Using itertools.starmap()
The itertools module of Python provides us a starmap() function which is used to apply a given function to each of the tuple of an iterable. This can be used to perform this task.
Python3
# Python3 code to demonstrate working of
# Tuple list cross multiplication
# using itertools.starmap()
# importing itertools for starmap()
import itertools
# initialize lists
test_list1 = [(2, 4), (6, 7), (5, 1)]
test_list2 = [(5, 4), (8, 10), (8, 14)]
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
# Tuple list cross multiplication
# using itertools.starmap()
res = list(itertools.starmap(lambda x,y : (x[0] * y[0], x[1] * y[1]), zip(test_list1, test_list2)))
# printing result
print("The multiplication across lists is : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy
OutputThe original list 1 : [(2, 4), (6, 7), (5, 1)]
The original list 2 : [(5, 4), (8, 10), (8, 14)]
The multiplication across lists is : [(10, 16), (48, 70), (40, 14)]
Time Complexity: O(n)
Space Complexity: O(n)
Method#4: using max() + zip() + recursion.
Python
# Python3 code to demonstrate working of
# Tuple list cross multiplication
# using max() + zip() + recursion
# getting Product
def prod(val) :
if len(val) == 1:
return val[0]
return val[0] * prod(val[1:])
# initialize lists
test_list1 = [(2, 4), (6, 7), (5, 1)]
test_list2 = [(5, 4), (8, 10), (8, 14)]
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
# Tuple list cross multiplication
# using max() + zip() + recursion
res = [tuple(map(prod, zip(a, b))) for a, b in zip(test_list1, test_list2)]
# printing result
print("The multiplication across lists is : " + str(res))
#this code contributed by tvsk
OutputThe original list 1 : [(2, 4), (6, 7), (5, 1)]
The original list 2 : [(5, 4), (8, 10), (8, 14)]
The multiplication across lists is : [(10, 16), (48, 70), (40, 14)]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #6: Using NumPy (without transposing)
Here's another approach using NumPy, but without transposing the result. Instead, we can directly reshape the result array to get the desired output format.
Steps:
- Import the numpy library.
- Convert the given lists to numpy arrays using np.array().
- Use np.multiply() function to perform element-wise multiplication of arrays.
- Use np.reshape() function to reshape the result array to the desired format.
Python3
import numpy as np
# initialize lists
test_list1 = [(2, 4), (6, 7), (5, 1)]
test_list2 = [(5, 4), (8, 10), (8, 14)]
# convert lists to numpy arrays
arr1 = np.array(test_list1)
arr2 = np.array(test_list2)
# perform element-wise multiplication
res_arr = np.multiply(arr1, arr2)
# reshape the result array to tuple list format
res = res_arr.reshape(-1, 2).tolist()
# printing result
print("The multiplication across lists is : " + str(res))
OUTPUT:
The multiplication across lists is : [[10, 16], [48, 70], [40, 14]]
Time Complexity: O(n)
Auxiliary Space: O(n)
Similar Reads
Python | Tuple multiplication Sometimes, while working with records, we can have a problem in which we may need to perform multiplication of tuples. This problem can occur in day-day programming. Let's discuss certain ways in which this task can be performed. Method #1 : Using zip() + generator expression The combination of abov
5 min read
Python - Cross Pairing in Tuple List Given 2 tuples, perform cross pairing of corresponding tuples, convert to single tuple if 1st element of both tuple matches. Input : test_list1 = [(1, 7), (6, 7), (8, 100), (4, 21)], test_list2 = [(1, 3), (2, 1), (9, 7), (2, 17)] Output : [(7, 3)] Explanation : 1 occurs as tuple element at pos. 1 in
5 min read
Python - Constant Multiplication over List We are given a list we need to multiply each element in the list by a constant. For example, we are having a list a = [1, 2, 3, 4, 5] and constant c=2 we need to multiply this constant in whole list.Using List ComprehensionList comprehension allows us to multiply each element in list by a constant b
3 min read
Python - List of tuples to multiple lists Converting a list of tuples into multiple lists involves separating the tuple elements into individual lists. This can be achieved using methods like zip(), list comprehensions or loops, each offering a simple and efficient way to extract and organize the data.Using zip()zip() function is a concise
3 min read
Python | Custom Multiplication in list of lists Sometimes, when we are fed with the list of list, we need to multiply each of its element list with a particular element fed by the order in the other list. This particular problem is very specific but knowledge of it can be useful in such cases. Let's discuss certain ways in which this can be done.
8 min read