Q:

How to multiply all elements in list Python?

0

 How to multiply all elements in list Python

In this exercise, you will learn how to multiply all elements in a list in Python.

A list is a sequence of indexed and ordered values like an array. It is mutable, which means we can change the order of elements in a list. A list in Python is a linear data structure that can hold heterogeneous elements. It is flexible to shrink and grow, and there is no need to declare it.

There are different ways in Python to multiply all the elements in a list.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Using traversal method

Here, we have taken a variable 'product' and initialize the value 1. Next, we have traversed the list till the end and multiply every element with the product. The value of 'product' at last is the final result.

# Python program to multiply all elements in the
# list using traversal
 
def multiplyList(xList) :
     
    product = 1
    for x in xList:
         product = product * x
    return product
     
list1 = [11, 3, 2]
list2 = [8, 2, 5]

print("Product of first list: ",multiplyList(list1))
print("Product of second list: ",multiplyList(list2))

Output of the above code: 

Product of first list:  66
Product of second list:  80

Using numpy.prod()

Numpy prod() method returns the product of the array of elements over a given axis. This method returns an integer or a float value depending on the multiplication result. In the given example, we import the numpy module and use the np.prod() method to get the product of all elements of the list.

# Python program to multiply all elements in the
# list using numpy

import numpy as np

list1 = [6, 4, 2]
list2 = [8, 2, 5]
 
# Multiply elements of list using numpy.prod()
product1 = np.prod(list1)
product2 = np.prod(list2)

print("Product of first list: ",product1)
print("Product of second list: ",product2)

Output of the above code: 

Product of first list:  48
Product of second list:  80

Using functools.reduce()

The reduce() function is defined in the functools module. It accepts a function in the first argument and an iterable in the second argument. We can use this function to multiply all the elements of the list by providing operator.mul in the first argument and list in the second.

# Python program to multiply all elements in the
# list using functools and operator

# importing functools for reduce()
import functools

# importing operator for operator functions
import operator

a_list = [5, 2, 7, 4]

product = functools.reduce(operator.mul, a_list)

print("Product of elements of the list: ",product)

Output of the above code: 

Product of elements of list:  280

Using math.prod

We can also use the math.prod() method to easily multiply the elements of the list. The math.prod is a new function and available from Python 3.8.

# Import math library
import math

list1 = [11, 2, 4]
list2 = [3, 6, 5]
 
 
result1 = math.prod(list1)
result2 = math.prod(list2)

print("Product of first list: ",result1)
print("Product of second list: ",result2)

 Output of the above code: 

Product of first list:  88
Product of second list:  90

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python Exercises, Practice Questions and Solutions

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How do you count consonants in a string in python?... >>
<< How do you find the GCF in Python?...