How to implement this code with recursive function

how to find the Product of All Elements in a list (t) using recursive function.

import rhinoscriptsyntax as rs
from itertools import combinations, product
f=
a = [1 for _ in range(12)]
b = [2 for _ in range(12)]
c = [3 for _ in range(12)]
t = a + b + c
for _ in range(4):
for i in combinations(t,1):
f.append(i)
for i in product(t,repeat =2):
f.append(i)
for _ in range(8):
for i in product(t,repeat=3):
f.append(i)
for i in product(t,repeat = 4):
f.append(i)
for i in product(t,repeat =5):
f.append(i)

This can be accomplished many ways, but using recursion and list slicing, you can for instance do this:

def mass_product(lt):
    if len(lt) == 0:
        return 1
    return lt[0] * mass_product(lt[1:])

if __name__ == "__main__":
    a = [1 for _ in xrange(12)]
    b = [2 for _ in xrange(12)]
    c = [3 for _ in xrange(12)]
    t = a + b + c
    
    print(mass_product(t))  # 2176782336
1 Like