Python List Multiplication Program

Write a Python program to perform list multiplication using for loop range. In this example, we allow users to enter the list items. Next, we used the for loop range (for i in range(listNumber)) to iterate the multiList list items. Within the loop, we are multiplying each list item and printing the result.

multiList = []

listNumber = int(input("Enter the Total List Items = "))
for i in range(1, listNumber + 1):
    listValue = int(input("Enter the %d list Item = " %i))
    multiList.append(listValue)

print("List Items = ", multiList)

listMultiplication = 1

for i in range(listNumber):
    listMultiplication = listMultiplication * multiList[i]
    

print("The Muliplication of all teh List Items = ", listMultiplication)
List Multiplication Program

Python List Multiplication Program using For loop

multiList = []

listNumber = int(input("Enter the Total List Items = "))
for i in range(1, listNumber + 1):
    listValue = int(input("Enter the %d List Item = " %i))
    multiList.append(listValue)

print("List Items = ", multiList)

listMulti = 1

for num in multiList:
    listMulti = listMulti * num

print(listMulti)
Enter the Total List Items = 5
Enter the 1 List Item = 10
Enter the 2 List Item = 4
Enter the 3 List Item = 9
Enter the 4 List Item = 11
Enter the 5 List Item = 7
List Items =  [10, 4, 9, 11, 7]
27720

This program performs the list multiplication using a While loop.

multiList = []

listNumber = int(input("Enter the Total Items = "))
for i in range(1, listNumber + 1):
    listValue = int(input("Enter the %d Item = " %i))
    multiList.append(listValue)

print("List Items = ", multiList)

listMultiplication = 1
i = 0

while (i < listNumber):
    listMultiplication = listMultiplication * multiList[i]
    i = i + 1
    
print("Result = ", listMultiplication)
Enter the Total Items = 4
Enter the 1 Item = 9
Enter the 2 Item = 10
Enter the 3 Item = 2
Enter the 4 Item = 4
List Items =  [9, 10, 2, 4]
The Result =  720

In this list example, we created a listMultiplication(multiList) function that returns the result go list multiplication.

def listMultiplication(multiList):
    listMulti = 1

    for num in multiList:
        listMulti = listMulti * num
    return listMulti

multiList = [10, 20, 8]

listMultip = listMultiplication(multiList)

print("The Result = ", listMultip)
The Result =  1600

Program for List Multiplication using built-in functions

Using math library

The math library has the built-in prod() function that helps to multiply all the elements in a list and returns the output.

import math

a = [2, 3, 4, 5, 6]

mul = math.prod(a)

print(mul)
720

Using numpy module

The popular numpy module has the prod() function, which will also perform the list multiplication, and this program uses the same.

import numpy as np

a = [2, 3, 4, 5, 6]

mul = np.prod(a)
print(mul)
720

Using reduce() and operator

By default, the mul() function in the operator module accepts the arguments, multiplies those values, and returns the output. If you combine it with the reduce function, the multiplication process will apply to all the list items.

from functools import reduce
import operator

a = [2, 3, 2, 4, 5, 6]

mul = reduce(operator.mul, a)
print(mul)
1440