Change language

Python | Multiply all the numbers in the list (3 different ways)

|

Examples:

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

Method 1: workaround

Initialize the product value to 1 (not 0, if 0 multiplied by anything returns zero). Go to the end of the list, multiply each number by the product. The value stored in the product at the end will give you your final answer.

Below this Python implementation of the above approach:

# Python program for multiply all values ​​in
# list using traversal

 

def multiplyList (myList):

 

  # Multiply elements one at a time

result = 1

for x in myList:

< p> result = result *

return result 

 
# Driver code

list1 = [ 1 , 2 , 3

list2 = [ 3 , 2 , 4 ]

print (multiplyList (list1))

print (multiplyList (list2) )

Output:

 6 24 

Method 2: Using numpy.prod ()

We can use numpy.prod () from import numpy to get the multiplication of all numbers in the list. Returns an integer or floating point value depending on the result of the multiplication.

Below is Python3 implementation of the above approach:

# Python3 multiplication program all values ​​in
# list using numpy.prod ()

 

import numpy 

list1 = [ 1 , 2 , 3

list2 = [ 3 , 2 , 4 ]

 
# using numpy.prod () to get multiplications

result1 = numpy.prod (list1)

result2 = numpy.prod (list2)

print (result1)

print ( result2)

Output:

 6 24 

Method 3 of 3: Using a lambda function: Using numpy.array

Lambda definition not contains a return statement, it always contains the expression that is returned. We can also put a lambda definition wherever a function is expected and we don’t need to assign it to a variable at all. This is the simplicity of lambda functions. The Reduce () function in Python takes a function and a list as an argument. The function is called with a lambda function and a list, and returns the result minified by ew . This performs an iterative operation on pairs of the list.

Below is the implementation of the above approach in Python3:

# Python3 program to multiply all values ​​in
# list using lambda function and Reduce ()

 

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)

Exit:

 6 24 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method