Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a built-in product() in Python? [duplicate]

I've been looking through a tutorial and book but I can find no mention of a built in product function i.e. of the same type as sum(), but I could not find anything such as prod().

Is the only way I could find the product of items in a list by importing the mul() operator?

like image 983
George Burrows Avatar asked Oct 30 '11 22:10

George Burrows


People also ask

Is there a product function in python?

prod() method in Python is used to calculate the product of all the elements present in the given iterable. Most of the built-in containers in Python like list, tuple are iterables. The iterable must contain numeric value else non-numeric types may be rejected.

What is a product in python?

product() is a part of a python module itertools; a collection of tools used to handle iterators. Together all these tools form iterator algebra. Itertools will make your code stand out. Above all, it will make it more pythonic.

How do I take a product in python?

We can use numpy. prod() from import numpy to get the multiplication of all the numbers in the list. It returns an integer or a float value depending on the multiplication result.

How do you multiply numbers in python?

In python, to multiply number, we will use the asterisk character ” * ” to multiply number. After writing the above code (how to multiply numbers in Python), Ones you will print “ number ” then the output will appear as a “ The product is: 60 ”. Here, the asterisk character is used to multiply the number.


2 Answers

Pronouncement

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

Python 3.8 Update

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

>>> from math import prod >>> prod(range(1, 11)) 3628800 

Alternative with reduce()

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

def prod(iterable):     return reduce(operator.mul, iterable, 1)  >>> prod(range(1, 5)) 24 

In Python 3, the reduce() function was moved to the functools module, so you would need to add:

from functools import reduce 

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 
like image 97
Raymond Hettinger Avatar answered Sep 18 '22 21:09

Raymond Hettinger


There is no product in Python, but you can define it as

def product(iterable):     return reduce(operator.mul, iterable, 1) 

Or, if you have NumPy, use numpy.product.

like image 39
Fred Foo Avatar answered Sep 20 '22 21:09

Fred Foo