Multiply every element in list python

You can just use a list comprehension:

my_list = [1, 2, 3, 4, 5]
my_new_list = [i * 5 for i in my_list]

>>> print(my_new_list)
[5, 10, 15, 20, 25]

Note that a list comprehension is generally a more efficient way to do a for loop:

my_new_list = []
for i in my_list:
    my_new_list.append(i * 5)

>>> print(my_new_list)
[5, 10, 15, 20, 25]

As an alternative, here is a solution using the popular Pandas package:

import pandas as pd

s = pd.Series(my_list)

>>> s * 5
0     5
1    10
2    15
3    20
4    25
dtype: int64

Or, if you just want the list:

>>> (s * 5).tolist()
[5, 10, 15, 20, 25]

Finally, one could use map, although this is generally frowned upon.

my_new_list = map(lambda x: x * 5, my_list)

Using map, however, is generally less efficient. Per a comment from ShadowRanger on a deleted answer to this question:

The reason "no one" uses it is that, in general, it's a performance pessimization. The only time it's worth considering map in CPython is if you're using a built-in function implemented in C as the mapping function; otherwise, map is going to run equal to or slower than the more Pythonic listcomp or genexpr (which are also more explicit about whether they're lazy generators or eager list creators; on Py3, your code wouldn't work without wrapping the map call in list). If you're using map with a lambda function, stop, you're doing it wrong.

And another one of his comments posted to this reply:

Please don't teach people to use map with lambda; the instant you need a lambda, you'd have been better off with a list comprehension or generator expression. If you're clever, you can make map work without lambdas a lot, e.g. in this case, map((5).__mul__, my_list), although in this particular case, thanks to some optimizations in the byte code interpreter for simple int math, [x * 5 for x in my_list] is faster, as well as being more Pythonic and simpler.

Given a list, print the value obtained after multiplying all numbers in a list. 

Examples: 

Input :  list1 = [1, 2, 3] 
Output : 6 
Explanation: 1*2*3=6 
Input : list1 = [3, 2, 4] 
Output : 24 

Method 1: Traversal

Initialize the value of the product to 1(not 0 as 0 multiplied with anything returns zero). Traverse till the end of the list, multiply every number with the product. The value stored in the product at the end will give you your final answer.

Below is the Python implementation of the above approach:  

Python

def multiplyList(myList):

    result = 1

    for x in myList:

        result = result * x

    return result

list1 = [1, 2, 3]

list2 = [3, 2, 4]

print(multiplyList(list1))

print(multiplyList(list2))

Method 2: Using numpy.prod()

We can use numpy.prod() from import numpy to get the multiplication of all the numbers in the list. It returns an integer or a float value depending on the multiplication result.

Below is the Python3 implementation of the above approach:  

Python3

import numpy

list1 = [1, 2, 3]

list2 = [3, 2, 4]

result1 = numpy.prod(list1)

result2 = numpy.prod(list2)

print(result1)

print(result2)

Output: 
 

6
24 

Method 3 Using lambda function: Using numpy.array

Lambda’s definition does not include a “return” statement, it always contains an expression that is returned. We can also put a lambda definition anywhere a function is expected, and we don’t have to assign it to a variable at all. This is the simplicity of lambda functions. The reduce() function in Python takes in a function and a list as an argument. The function is called with a lambda function and a list and a new reduced result is returned. This performs a repetitive operation over the pairs of the list.

Below is the Python3 implementation of the above approach:  

Python3

from functools import reduce

list1 = [1, 2, 3]

list2 = [3, 2, 4]

result1 = reduce((lambda x, y: x * y), list1)

result2 = reduce((lambda x, y: x * y), list2)

print(result1)

print(result2)

Method 4 Using prod function of math library: Using math.prod

Starting Python 3.8, a prod function has been included in the math module in the standard library, thus no need to install external libraries.

Below is the Python3 implementation of the above approach:  

Python3

import math

list1 = [1, 2, 3]

list2 = [3, 2, 4]

result1 = math.prod(list1)

result2 = math.prod(list2)

print(result1)

print(result2)

Output: 
 

6
24 

Method 5: Using mul() function of operator module. 

First we have to import the operator module then using the mul() function of operator module multiplying the all values in the list. 

Python3

from operator import*

list1 = [1, 2, 3]

m = 1

for i in list1:

    m = mul(i, m)

print(m)

Method 6: Using traversal by index

Python3

def multiplyList(myList) :

    result = 1

    for i in range(0,len(myList)):

        result = result * myList[i]

    return result

list1 = [1, 2, 3]

list2 = [3, 2, 4]

print(multiplyList(list1))

print(multiplyList(list2))

Method 7: Using itertools.accumulate

Python

from itertools import accumulate

list1 = [1, 2, 3]

list2 = [3, 2, 4]

result1 = list(accumulate(list1, (lambda x, y: x * y)))

result2 = list(accumulate(list2, (lambda x, y: x * y)))

print(result1[-1])

print(result2[-1])

Output:

6
24

How do you multiply all numbers in a list using for loops in Python?

Use a for loop to multiply all values in a list.
a_list = [2, 3, 4].
product = 1..
for item in a_list: Iterate over a_list..
product = product * item. multiply each element..
print(product).

How do you multiply all values in a numpy array?

A Quick Introduction to Numpy Multiply You can use np. multiply to multiply two same-sized arrays together. This computes something called the Hadamard product. In the Hadamard product, the two inputs have the same shape, and the output contains the element-wise product of each of the input values.

How do you multiply an array in Python?

multiply() in Python. numpy. multiply() function is used when we want to compute the multiplication of two array. It returns the product of arr1 and arr2, element-wise.

Can list be multiplied in Python?

Lists and strings have a lot in common. They are both sequences and, like pythons, they get longer as you feed them. Like a string, we can concatenate and multiply a Python list.