Python – Row Summation of Like Index Product

Given a Matrix and elements list, For every row, extract the summation of product of elements with argument list.
Input : test_list = [[4, 5], [1, 5], [8, 2]], mul_list = [5, 2, 3]
Output : [30, 15, 44]
Explanation : For 1st row, (4*5) + (5*2) = 30, as value of 1st element of result list. This way each element is computed.Input : test_list = [[4, 5, 8, 2]], mul_list = [5, 2, 3, 9]
Output : [72]
Explanation : Similar computation as above method. Just 1 element as single Row.
Method #1 : Using loop This is brute force way to solve this problem. In this, we perform iteration of each row and perform required summation in product using brute force approach using loop.
Python3
| # Python3 code to demonstrate working of # Row Summation of Like Index Product# Using loop# initializing listtest_list =[[3, 4, 5], [1, 7, 5], [8, 1, 2]] # printing original listprint("The original listis: " +str(test_list))# initializing mul list  mul_list =[5, 2, 3]# Using loopres =[]forsub intest_list:    sum=0    foridx, ele inenumerate(sub):                # performing summation of product with list elements        sum=sum+(ele *mul_list[idx])        # adding each row sum    res.append(sum)# printing result print("Listafter multiplication : " +str(res)) | 
The original list is : [[3, 4, 5], [1, 7, 5], [8, 1, 2]] List after multiplication : [38, 34, 48]
Time complexity: O(nm), where n is the number of rows and m is the number of columns in the matrix.
Auxiliary Space: O(n), where n is the number of rows in the matrix, for storing the row sums in the result list.
Method #2 : Using map() + sum() + zip() + lambda The combination of above functions can be used to solve this problem. In this, we perform the task of performing summation using sum(), zip() is used to map multiplication list with row values, and logic encapsulated and extended to each row, and its element using lambda and map().
Python3
| # Python3 code to demonstrate working of # Row Summation of Like Index Product# Using map() + sum() + zip() + lambda# initializing listtest_list =[[3, 4, 5], [1, 7, 5], [8, 1, 2]] # printing original listprint("The original listis: " +str(test_list))# initializing mul list  mul_list =[5, 2, 3]# Using map() + sum() + zip() + lambda# Performing product in inner map, by zipping with elements list, and sum at outer map() used.res =list(map(sum, (map(lambdaele: ([x *y forx, y inzip(                              ele, mul_list)]), test_list))))# printing result print("Listafter multiplication : " +str(res)) | 
The original list is : [[3, 4, 5], [1, 7, 5], [8, 1, 2]] List after multiplication : [38, 34, 48]
Time complexity: O(nm), where n is the number of rows and m is the number of columns in the matrix.
Auxiliary Space: O(n), where n is the number of rows in the matrix, for storing the row sums in the result list.
Method #3: Using list comprehension
Python3
| # Initializing listtest_list =[[3, 4, 5], [1, 7, 5], [8, 1, 2]] # Printing original listprint("The original list is: "+str(test_list))# Initializing mul listmul_list =[5, 2, 3]# Using list comprehensionres =[sum([test_list[i][j] *mul_list[j] forj inrange(len(mul_list))]) fori inrange(len(test_list))]# Printing resultprint("List after multiplication: "+str(res)) | 
The original list is: [[3, 4, 5], [1, 7, 5], [8, 1, 2]] List after multiplication: [38, 34, 48]
Time complexity: O(n^2), where n is the length of the input list. 
Auxiliary space: O(n), where n is the length of the input list. 
 
				 
					


