Open In App

Python – Cumulative Records Product

Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with data in form of records, we can have a problem in which we need to find the product element of all the records received. This is a very common application that can occur in Data Science domain. Let’s discuss certain ways in which this task can be performed.

Method #1 : Using loop + generator expression 
This is the most basic method to achieve solution to this task. In this, we iterate over whole nested lists using generator expression and get the product element using explicit product function.
 

Python3




# Python3 code to demonstrate working of
# Cumulative Records Product
# using loop + generator expression
 
# getting Product
def prod(val) :
    res = 1
    for ele in val:
        res *= ele
    return res
 
# initialize list
test_list = [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]
 
# printing original list
print("The original list : " + str(test_list))
 
# Cumulative Records Product
# using loop + generator expression
res = prod(int(j) for i in test_list for j in i)
 
# printing result
print("The Cumulative product of list is : " + str(res))


Output

The original list : [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]
The Cumulative product of list is : 5644800

Time complexity: O(nm), where n is the number of sublists and m is the length of each sublist.
Auxiliary space: O(1), as we are only using a few variables to store the intermediate results.

 
Method #2 : Using loop + map() + chain.from_iterable() 
The combination of above methods can also be used to perform this task. In this, the extension of finding product is done by combination of map() and from_iterable().
 

Python3




# Python3 code to demonstrate working of
# Cumulative Records Product
# using product + map() + chain.from_iterable()
from itertools import chain
 
# getting Product
def prod(val) :
    res = 1
    for ele in val:
        res *= ele
    return res
 
# initialize list
test_list = [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]
 
# printing original list
print("The original list : " + str(test_list))
 
# Cumulative Records Product
# using product + map() + chain.from_iterable()
res = prod(map(int, chain.from_iterable(test_list)))
 
# printing result
print("The cumulative product of list is : " + str(res))


Output

The original list : [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]
The cumulative product of list is : 5644800

The time complexity of this program is O(nk), where n is the number of tuples in the input list and k is the maximum number of elements in a tuple. 

The auxiliary space complexity of this program is O(1), as it only uses a constant amount of memory to store the result and temporary variables.

Method #3: Using functools.reduce() and operator.mul

This approach uses the reduce function from the functools library along with the mul operator from the operator library to perform the product of all elements in the records. This method can handle any level of nesting.

Python3




from functools import reduce
import operator
 
# initialize list
test_list = [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]
 
# printing original list
print("The original list : " + str(test_list))
 
# flatten the list
# Cumulative Records Product
# using functools.reduce() and operator.mul
res = reduce(operator.mul, [j for i in test_list for j in i])
 
# printing result
print("The cumulative product of list is : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy


Output

The original list : [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]
The cumulative product of list is : 5644800

Time complexity of the above method is O(n) and an auxiliary space is O(n) where n is the number of elements in the records.

Method #3: Using  a for loop:

Python3




test_list = [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]
res = 1
# printing original list
print("The original list : " + str(test_list))
  
for i in test_list:
    for j in i:
        res *= j
print("The cumulative product of list is : " + str(res))
#This code is contributed by Jyothi pinjala.


Output

The original list : [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]
The cumulative product of list is : 5644800

Time Complexity: O(N)
Auxiliary Space:  O(1)

Method #4: Using numpy.cumprod()

Another method to calculate the cumulative product of a list is by using the cumprod() function provided by the numpy library. This method can be faster and more efficient than the previous methods, especially for larger lists.

Here’s how to implement it step by step:

  1. Import the numpy library.
  2. Initialize the list.
  3. Convert the list to a numpy array using np.array().
  4. Use np.cumprod() to calculate the cumulative product of the array.
  5. Access the last element of the resulting array to get the cumulative product of the original list.

Python3




import numpy as np
 
# initialize list
test_list = [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]
 
# convert list to numpy array
test_array = np.array(test_list)
 
# calculate cumulative product
cumulative_product = np.cumprod(test_array)
 
# access last element for final result
result = cumulative_product[-1]
 
# print result
print("The cumulative product of the list is:", result)


OUTPUT: 

The cumulative product of the list is: 5644800

Time complexity: O(n), where n is the number of elements in the list.
Auxiliary space: O(n), as the numpy array of the same size as the original list is created.



Last Updated : 17 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads