Open In App

Python | Tuple list cross multiplication

Last Updated : 09 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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


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) 
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


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) 
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:

  1. Import the numpy library.
  2. Convert the given lists to numpy arrays using np.array().
  3. Use np.multiply() function to perform element-wise multiplication of arrays.
  4. 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)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads