Python - Row Summation of Like Index Product
Last Updated :
02 May, 2023
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 list
test_list = [[3, 4, 5], [1, 7, 5], [8, 1, 2]]
# printing original list
print("The original list is : " + str(test_list))
# initializing mul list
mul_list = [5, 2, 3]
# Using loop
res = []
for sub in test_list:
sum = 0
for idx, ele in enumerate(sub):
# performing summation of product with list elements
sum = sum + (ele * mul_list[idx])
# adding each row sum
res.append(sum)
# printing result
print("List after multiplication : " + str(res))
Output : 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 list
test_list = [[3, 4, 5], [1, 7, 5], [8, 1, 2]]
# printing original list
print("The original list is : " + 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(lambda ele: ([x * y for x, y in zip(
ele, mul_list)]), test_list))))
# printing result
print("List after multiplication : " + str(res))
Output : 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 list
test_list = [[3, 4, 5], [1, 7, 5], [8, 1, 2]]
# Printing original list
print("The original list is: " + str(test_list))
# Initializing mul list
mul_list = [5, 2, 3]
# Using list comprehension
res = [sum([test_list[i][j] * mul_list[j] for j in range(len(mul_list))]) for i in range(len(test_list))]
# Printing result
print("List after multiplication: " + str(res))
OutputThe 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.
Similar Reads
Python - Product of elements using Index list
Accessing an element from its index is easier task in python, just using the [] operator in a list does the trick. But in certain situations we are presented with tasks when we have more than once indices and we need to get all the elements corresponding to those indices and then perform the multipl
7 min read
Python - Find Product of Index Value and find the Summation
Given a List of elements, write a Python program to perform the product of index and value and compute the summation. Examples: Input : test_list = [5, 3, 4, 9, 1, 2] Output : 76 Explanation : 5 + (3*2) 6 + 12 + 36 + 5 + 12 = 76 Input : test_list = [5, 3, 4] Output : 23 Explanation : 5 + (3*2) 6 + 1
5 min read
Python - Index Value Summation List
To access the elements of lists, there are various methods. But sometimes we may require to access the element along with the index on which it is found and compute its summation, and for that, we may need to employ different strategies. This article discusses some of those strategies. Method 1: Nai
4 min read
Python - Suffix Product in list
We are given a list of numbers, and our task is to compute the suffix product for each position in the list. The suffix product at any index is the product of all elements to the right, including the element at that index. For example, given a = [2, 3, 4, 5], the suffix products would be [120, 60, 2
2 min read
Python - Consecutive Row summation in Matrix
This particular article focuses on a problem that has utility in competitive as well as day-day programming. Sometimes, we need to get the sum between the like indices when compared with the next list. The sum between the like elements in that index is returned. Letâs discuss certain ways in which t
5 min read
Python | Common Row elements Summation
The problem of finding the common elements in the list of 2 lists is quite a common problem and can be dealt with with ease and also has been discussed many times. But sometimes, we require to find the elements that are in common with N lists and return their sum. Let us discuss certain ways in whic
6 min read
Python | Record Index Product
Sometimes, while working with records, we can have a problem in which we need to multiply all the columns of a container of lists that are tuples. This kind of application is common in web development domain. Letâs discuss certain ways in which this task can be performed. Method #1 : Using loop + li
4 min read
Python | Summation of tuples in list
Sometimes, while working with records, we can have a problem in which we need to find the cumulative sum of all the values that are present in tuples. This can have applications in cases in which we deal with a lot of record data. Let's discuss certain ways in which this problem can be solved. Metho
7 min read
Python | K Value Indices Product
Usually, we require to find the index, in which the particular value is located. There are many method to achieve that, using index() etc. But sometimes require to find all the indices of a particular value in case it has multiple occurrences in list and compute their product. Lets discuss certain w
7 min read
Python - Product and Inter Summation dictionary values
Sometimes, while working with Python dictionaries, we can have a problem in which we need to perform product of entire value list and perform summation of product of each list with other. This kind of application in web development and day-day programming. Lets discuss certain ways in which this tas
4 min read