Given the List of integers as input to the Python program, your task is to write a Python program having a function that will calculate product of all the numbers present in a list by multiplying each and every number present in list
List of numbers can be simple lists or nested lists. let's look into both the solutions
Write A Python Function To Multiply All Numbers In A Nested List
Define a function list_product() pass the nested list of integers as input argument. Initialize the product variable to 1 This product variable will be used to find the product of all the integers present in the nested list.
Use the following to iterate all the items present in the list. Using the if statement check if instance is a sublist, then call the same function recursively.
If the instance is an integer multiply it with product variable
def list_product(nested_list):
product = 1
for item in nested_list:
if isinstance(item, list):
product = product * list_product(item)
elif isinstance(item, int):
product = product * item
return product
Take a nested list as input, containing the integer values as shown below. it contains 1 nested list and inside the national is it contains another sub list.
Call the function to find the product of the nested list and store the result in a variable. Print the product of list of integers using print function call.
numbers = [1, 2, 3, [4, 5, 6, [7, 8]], 9]
result = list_product(numbers)
print("Product of List Item is : ", result)
Complete program
======================
def list_product(nested_list):
product = 1
for item in nested_list:
if isinstance(item, list):
product = product * list_product(item)
elif isinstance(item, int):
product = product * item
return product
numbers = [1, 2, 3, [4, 5, 6, [7, 8]], 9]
result = list_product(numbers)
print("Product of List Item is : ", result)
Output
============
Product of List Item is : 362880
Write A Python Function To Multiply All Numbers In A \List
Let's consider a function list_product() passing the list of integers as input to this function as an argument. Take a local variable product and set it to 1 iterate all the numbers present in the list and multiply with the product what is the for loop is complete return the product
def list_product(my_list):
product = 1
for item in my_list:
product = product * item
return product
In the main program take a variable length and ask a user to enter the list length, take an empty list number and set it to empty. using the for loop to iterate from 0 to N size of the list, keep asking the user to enter the list item, convert into an integer and append it to a list of numbers.
length = int(input("Enter List Length :"))
numbers = []
for temp in range(length):
item = int(input("Enter List Item : "))
numbers += [item, ]
Take a variable result and call the function store the product of the list of integers that the function returns into the result. finally print the result using print function call.
result = list_product(numbers)
print("Product of List Item is : ", result)
Complete Program
=======================
def list_product(my_list):
product = 1
for item in my_list:
product = product * item
return product
length = int(input("Enter List Length :"))
numbers = []
for temp in range(length):
item = int(input("Enter List Item : "))
numbers += [item, ]
result = list_product(numbers)
print("Product of List Item is : ", result)
OUTPUT
===============
Enter List Length :3
Enter List Item : 1
Enter List Item : 2
Enter List Item : 3
Product of List Item is : 6
Conclusion
=============
Run above program by providing the random list values try to evaluate the correctness of the above program
Comment down below if you have any suggestions to improve the program.
0 Comments