Python | Incremental Records Product

Sometimes, while working with data, we can have a problem in which we need to find accumulative product of each index in tuples. This problem can have application in web development and competitive programming domain. Let’s discuss certain way in which this problem can be solved.

Method : Using accumulate() + loop + lambda + map() + tuple() + zip()
The combination of above functions can be used to solve this task. In this, we pair the elements using zip(), the then we perform the product of them and we extend this to all elements using map(). The taking forward of product is done by using accumulate. The binding of all logic is done by lambda function.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Incremental Records Product
# Using accumulate() + loop + lambda + map() + tuple() + zip()
from itertools import accumulate
  
def prod(val) :     
    res = 1         
    for ele in val:         
        res *= ele         
    return res
      
# initialize list
test_list = [(3, 4, 5), (4, 5, 7), (1, 4, 10)]
  
# printing original list 
print("The original list : " + str(test_list))
  
# Incremental Records Product
# Using accumulate() + loop + lambda + map() + tuple() + zip()
res = list(accumulate(test_list, lambda i, j: tuple(map(prod, zip(i, j)))))
  
# printing result
print("Accumulative index product of tuple list : " + str(res))

chevron_right


Output :

The original list : [(3, 4, 5), (4, 5, 7), (1, 4, 10)]
Accumulative index product of tuple list : [(3, 4, 5), (12, 20, 35), (12, 80, 350)]
My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.