Is there a product function in python?

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.

Given a list, print the value obtained after multiplying all numbers in a list. 

Examples: 

Input :  list1 = [1, 2, 3] 
Output : 6 
Explanation: 1*2*3=6 
Input : list1 = [3, 2, 4] 
Output : 24 

Method 1: Traversal

Initialize the value of the product to 1(not 0 as 0 multiplied with anything returns zero). Traverse till the end of the list, multiply every number with the product. The value stored in the product at the end will give you your final answer.

Below is the Python implementation of the above approach:  

Python

def multiplyList(myList):

    result = 1

    for x in myList:

        result = result * x

    return result

list1 = [1, 2, 3]

list2 = [3, 2, 4]

print(multiplyList(list1))

print(multiplyList(list2))

Method 2: Using numpy.prod()

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.

Below is the Python3 implementation of the above approach:  

Python3

import numpy

list1 = [1, 2, 3]

list2 = [3, 2, 4]

result1 = numpy.prod(list1)

result2 = numpy.prod(list2)

print(result1)

print(result2)

Output: 
 

6
24 

Method 3 Using lambda function: Using numpy.array

Lambda’s definition does not include a “return” statement, it always contains an expression that is returned. We can also put a lambda definition anywhere a function is expected, and we don’t have to assign it to a variable at all. This is the simplicity of lambda functions. The reduce() function in Python takes in a function and a list as an argument. The function is called with a lambda function and a list and a new reduced result is returned. This performs a repetitive operation over the pairs of the list.

Below is the Python3 implementation of the above approach:  

Python3

from functools import reduce

list1 = [1, 2, 3]

list2 = [3, 2, 4]

result1 = reduce((lambda x, y: x * y), list1)

result2 = reduce((lambda x, y: x * y), list2)

print(result1)

print(result2)

Method 4 Using prod function of math library: Using math.prod

Starting Python 3.8, a prod function has been included in the math module in the standard library, thus no need to install external libraries.

Below is the Python3 implementation of the above approach:  

Python3

import math

list1 = [1, 2, 3]

list2 = [3, 2, 4]

result1 = math.prod(list1)

result2 = math.prod(list2)

print(result1)

print(result2)

Output: 
 

6
24 

Method 5: Using mul() function of operator module. 

First we have to import the operator module then using the mul() function of operator module multiplying the all values in the list. 

Python3

from operator import*

list1 = [1, 2, 3]

m = 1

for i in list1:

    m = mul(i, m)

print(m)

Method 6: Using traversal by index

Python3

def multiplyList(myList) :

    result = 1

    for i in range(0,len(myList)):

        result = result * myList[i]

    return result

list1 = [1, 2, 3]

list2 = [3, 2, 4]

print(multiplyList(list1))

print(multiplyList(list2))


Does Python have a product function?

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.

How do you write a product in Python?

Example:.
# Example Python program to find the product of elements from an iterable. import math..
# A python tuple. sequence = (1, 1.2, 2, 2.2, 3, 3.2);.
# Find the product of the elements of the tuple with the starting product value as 4. product = math.prod(sequence, start=4);.
print("Product:"); print(product);.

What is a product of a function?

Description. The PRODUCT function multiplies all the numbers given as arguments and returns the product. For example, if cells A1 and A2 contain numbers, you can use the formula =PRODUCT(A1, A2) to multiply those two numbers together.

How do you multiply a value in Python?

Multiplication in Python is done by ( * ) operator, Then the result is saved in the product variable and printed out using string formatting.