Python program to multiply all numbers in the list?


You will understand how to multiply every number in a list in Python in this article using various methods. A list is an ordered collection of values that are enclosed in square brackets. List contains values known as items that can be retrieved by their unique index. We'll create a function that multiplies each value in the list and outputs the result.

The result of multiplying all the values is a single thing. For instance, the result will be 36 for the list [3,2,6]. We will go over the various methods for calculating the sum of all the numbers in a list.

Using math.prod() function

In this method, the product is calculated using the math module's prod() function. In our program, the math module gives users access to numerous mathematical operations including pow(), sum(), and avg().

Algorithm

Following is an approach to multiply all numbers in the list using math.prod() function −

  • Import the module.

  • Define a function for number multiplication.

  • Then return math.prod (list).

  • Create a list.

  • Call the function and pass the list.

  • Print the value that the function returned.

Example

Following is an example to multiply all numbers in the list using math.prod() function −

#importing math module import numpy def multiply_numbers(list): return numpy.prod(list) given_list = [2,5,3,7,4,85,-3] print('The list is:',given_list) print("The product is: ") print(multiply_numbers(given_list))

Output

Following is an output of the above code −

The list is: [2, 5, 3, 7, 4, 85, -3]
The product is: 
-214200

Using numpy.prod() function

In this method, the product is calculated using the NumPy module's prod() function. It enables a programmer to work with massive quantities of data and numerous advanced mathematical calculations.

Algorithm

Following is an approach to multiply all numbers in the list using numpy.prod() function −

  • Import the module.
  • Define a function for number multiplication.
  • Then return numpy.prod (list).
  • Create a list.
  • Call the function and pass the list.
  • Print the value that the function returned.

Example

Following is an example to multiply all numbers in the list using numpy.prod() function −

#importing math module import numpy def multiply_numbers(list): return numpy.prod(list) given_list = [2,1,3,7,4,85,3] print('The list is:',given_list) print("The product is: ") print(multiply_numbers(given_list))

Output

Following is an output of the above code −

The list is: [2, 1, 3, 7, 4, 85, 3]
The product is: 
42840

Using for loop

In this method, we'll search over the entire list until we find the item. Each integer in the list should be multiplied by a variable product, which should be initialised to 1, to obtain the answer. The Python language's for loop will be used to access each integer in the list.

Algorithm

Following is an approach to multiply all numbers in the list using for loop −

  • Define a function for number multiplication.
  • Set a variable product to 1 after declaring it.
  • For each element in the list, execute a loop.
  • Multiply each element by the product.
  • Product return.
  • Create a list.
  • Pass the list through our function.
  • Print the value that the function returned.

Example

Following is an example to multiply all numbers in the list using for loop −

def multiply_numbers(list): prod = 1 for i in list: prod = prod*i return prod given_list = [2,1,3,7,4,85,3] print('The list is:',given_list) print("The product is: ") print(multiply_numbers(given_list))

Output

Following is an output of the above code −

The list is: [2, 1, 3, 7, 4, 85, 3]
The product is: 
42840

Using reduce() function

The reduce() method from the functools package can be imported. It accepts three parameters: a function, a sequence, and an initial value[optional].

It first passes the function two data items taken from the sequence. The function receives the obtained result together with the third data item. Until there are no more data items in the iterator, this process is repeated.

Example

using lambda function

The product of every element in a list is calculated in the example that follows. Reduce() accepts list_1 as a parameter and a lambda function (lambda m, n: m * n).

The lambda function receives the elements of the given_list as arguments. The product value is returned by the lambda function after multiplying the arguments. The output of the reduce() function is a single value.

from functools import reduce given_list = [2,1,3,2,4,8,3] print('The list is:',given_list) product= print('The list is:',reduce((lambda m, n: m*n), given_list))

Output

Following is an output of the above code.

The list is: [2, 1, 3, 2, 4, 8, 3]
The list is: 1152

Using mul() function

Example

The operator module must first be imported before we may multiply all of the list's values using its mul() function.

from operator import* given_list = [2,-3,3,2,4,5,3] print('The list is:',given_list) p = 1 for i in given_list: # multiply all the elements in the given list p = mul(i, p) print(p)

Output

Following is an output of the above code −

The list is: [2, -3, 3, 2, 4, 5, 3]
-2160

Updated on: 23-Nov-2022

20K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements