Get the (multiplicative) product of a tuple or list?

I don't see any problem with using indexes here:

sum([x[0] * x[1] for x in combinations(args, 2)])

If you really want to avoid them, you can do:

sum([x*y for x,y in combinations(args, 2)])

But, to be honest I would prefer your commented out version. It is clear, readable and more explicit. And you don't really gain much by writing it as above just for three variables.

Is there a function I can use that acts like sum(), but only for multiplication?

Built-in? No. But you can get that functionality rather simply with the following:

In : a=[1,2,3,4,5,6]

In : from operator import mul

In : reduce(mul,a)
Out: 720

In short, just use np.prod

my_tuple = (2, 3, 10)
print(np.prod(my_tuple))  # 60

Which is in your use case

np.sum(np.prod(x) for x in combinations(args, 2))

np.prod can take both lists and tuple as a parameter. It returns the product you want.


Since this is in the top Google results, I'll just add that since Python 3.8, you can do :

from math import prod
t = (5, 10)
l = [2, 100]
prod(t) # 50
prod(l) # 200