Given a list, print the value obtained after multiplying all numbers in a list.
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 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 it the Python implementation of the above approach:
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 it the Python3 implementation of the above approach:
Method 3 Using lambda function: Using numpy.array
Lambda definition does not include a “return” statement, it always contains an expression which 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 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:
- Python | Multiply Integer in Mixed List of string and numbers
- Python - Multiply two list
- Python | Repeat and Multiply list extension
- Python - Multiply Consecutive elements in list
- Python - Multiply all cross list element pairs
- Python | Ways to sum list of lists and return sum list
- Python | Ways to Convert a 3D list into a 2D list
- Python | Ways to Spilt the list by some value
- Different ways to clear a list in Python
- Python | Ways to shuffle a list
- Python | Ways to rotate a list
- Python | Ways to flatten a 2D list
- Extending a list in Python (5 different ways)
- Python | Ways to find length of list
- Python | Ways to remove particular list element
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.