Patrick McElhaney
Patrick McElhaney

Reputation: 59331

What's the function like sum() but for multiplication? product()?

Python's sum() function returns the sum of numbers in an iterable.

sum([3,4,5]) == 3 + 4 + 5 == 12

I'm looking for the function that returns the product instead.

somelib.somefunc([3,4,5]) == 3 * 4 * 5 == 60

I'm pretty sure such a function exists, but I can't find it.

Upvotes: 273

Views: 229628

Answers (10)

Keenan
Keenan

Reputation: 439

If you don't want to import anything:

eval("*".join([str(num) for num in list_of_numbers]))

Upvotes: 0

ojrac
ojrac

Reputation: 13421

Historically, Guido vetoed the idea: http://bugs.python.org/issue1093

As noted in that issue, you can make your own:

from functools import reduce # Valid in Python 2.6+, required in Python 3
import operator

reduce(operator.mul, (3, 4, 5), 1)

Upvotes: 240

hipoglucido
hipoglucido

Reputation: 545

You can also encode your list of numbers as a pd.Series and then use pd.Series.product():

>>> import pandas as pd
>>> pd.Series([5,3,-1]).product()
-15

Upvotes: 1

Raymond Hettinger
Raymond Hettinger

Reputation: 226624

Update:

In Python 3.8, the prod function was added to the math module. See: math.prod().

Older info: Python 3.7 and prior

The function you're looking for would be called prod() or product() but Python doesn't have that function. So, you need to write your own (which is easy).

Pronouncement on prod()

Yes, that's right. Guido rejected the idea for a built-in prod() function because he thought it was rarely needed.

Alternative with reduce()

As you suggested, it is not hard to make your own using reduce() and operator.mul():

from functools import reduce  # Required in Python 3
import operator
def prod(iterable):
    return reduce(operator.mul, iterable, 1)

>>> prod(range(1, 5))
24

Note, in Python 3, the reduce() function was moved to the functools module.

Specific case: Factorials

As a side note, the primary motivating use case for prod() is to compute factorials. We already have support for that in the math module:

>>> import math

>>> math.factorial(10)
3628800

Alternative with logarithms

If your data consists of floats, you can compute a product using sum() with exponents and logarithms:

>>> from math import log, exp

>>> data = [1.2, 1.5, 2.5, 0.9, 14.2, 3.8]
>>> exp(sum(map(log, data)))
218.53799999999993

>>> 1.2 * 1.5 * 2.5 * 0.9 * 14.2 * 3.8
218.53799999999998

Note, the use of log() requires that all the inputs are positive.

Upvotes: 159

katiex7
katiex7

Reputation: 913

Perhaps not a "builtin", but I consider it builtin. anyways just use numpy

import numpy 
prod_sum = numpy.prod(some_list)

Upvotes: 8

S.Lott
S.Lott

Reputation: 391982

Use this

def prod(iterable):
    p = 1
    for n in iterable:
        p *= n
    return p

Since there's no built-in prod function.

Upvotes: 24

tommy.carstensen
tommy.carstensen

Reputation: 9622

I prefer the answers a and b above using functools.reduce() and the answer using numpy.prod(), but here is yet another solution using itertools.accumulate():

import itertools
import operator
prod = list(itertools.accumulate((3, 4, 5), operator.mul))[-1]

Upvotes: 4

zweiterlinde
zweiterlinde

Reputation: 14779

There isn't one built in, but it's simple to roll your own, as demonstrated here:

import operator
def prod(factors):
    return reduce(operator.mul, factors, 1)

See answers to this question:

Which Python module is suitable for data manipulation in a list?

Upvotes: 44

Benjamin
Benjamin

Reputation: 499

There's a prod() in numpy that does what you're asking for.

Upvotes: 49

Steve B.
Steve B.

Reputation: 57333

Numeric.product 

( or

reduce(lambda x,y:x*y,[3,4,5])

)

Upvotes: 29

Related Questions