Home » Python » Python programs

# Python program to multiply all numbers of a list

Here, we are going to learn **how to multiply all numbers of a list in Python?**

Submitted by Shivang Yadav, on April 05, 2021

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

List is a sequence data type. It is mutable as its values in the list can be modified. It is a collection of ordered sets of values enclosed in square brackets **[]**.

**Multiply all numbers of a list**

We will take a list as input from the user. And return the product of all numbers of the list.

**Example:**

Input: [4, 1, 6, 3, 9] Output: 648

We simply need to find the product of all numbers. This task can be performed in multiple ways in python.

**Method 1: Using loops**

To find the product of all elements of a list, we will simply traverse the list and then multiply all values to a product variable. At the end return the product.

**Algorithm:**

- Take input from the user.
- Loop over the array.
- productVal *= i.

- Return the productVal.

**Program to multiply all numbers of a list**

# Python program to multiply all numbers of a list # Getting list from user myList = [] length = int(input("Enter number of elements: ")) for i in range(0, length): value = int(input()) myList.append(value) # multiplying all numbers of a list productVal = 1 for i in myList: productVal *= i # Printing values print("List : ", myList) print("Product of all values = ", productVal)

**Output:**

Enter number of elements: 5 4 1 6 3 9 List : [4, 1, 6, 3, 9] Product of all values = 648

**Method 2: Using prod method from math library**

Python also provides its users with a special method to perform tasks.

One such method is *prod()* which accepts a list (or iterable) and returns the product of all its elements.

**Syntax:**

math.prod(list_name)

**Return** a value based on the type of list which is the product of all numbers of the list.

**Program to multiply all numbers of a list**

# Python program to multiply all numbers of a list import math # Getting list from user myList = [] length = int(input("Enter number of elements: ")) for i in range(0, length): value = int(input()) myList.append(value) # multiplying all numbers of a list productVal = math.prod(myList) # Printing values print("List : ", myList) print("Product of all values= ", productVal)

**Output:**

Enter number of elements: 3 3 5 7 List : [3, 5, 7] Product of all values= 105

**Method 3: Using numpy's prod() method**

You can alternatively use the method *prod()* from *numpy* library. As Python has many libraries that provide its users multiple functions to perform tasks quickly, you have multiple options to do it.

The *numpy.prod()* method takes in the list and returns the product of all values of the list.

**Syntax:**

numpy.prod(list_name)

**Program to multiply all numbers of a list**

# Python program to multiply all numbers of a list import numpy # Getting list from user myList = [] length = int(input("Enter number of elements: ")) for i in range(0, length): value = int(input()) myList.append(value) # multiplying all numbers of a list productVal = numpy.prod(myList) # Printing values print("List : ", myList) print("Product of all values= ", productVal)

**Output:**

Enter number of elements: 5 4 1 6 3 9 List : [4, 1, 6, 3, 9] Product of all values= 648

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions