Archive

Posts Tagged ‘product’

Product of the elements in a list

April 14, 2012 1 comment

Design goal: should be just one line, without using explicit iteration over the list.

Sum of the elements:

>>> li = [2,3,5]
>>> sum(li)
10

Product of the elements:

>>> li = [2,3,5]
>>> from operator import mul
>>> reduce(mul, li)
30
Categories: python Tags: , ,