JoeG
0
Q:

python multiply all elements in list

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]
2
# Python program to multiply all values in the 
# list using traversal 
#IF NOT USING NUMPY
def multiplyList(myList) : 
      
    # Multiply elements one by one 
    result = 1
    for x in myList: 
         result = result * x  
    return result  
      
# Driver code 
list1 = [1, 2, 3]  
list2 = [3, 2, 4] 
print(multiplyList(list1)) 
print(multiplyList(list2)) 
3
# Python3 program to multiply all values in the 
# list using numpy.prod() 
  
import numpy  
list1 = [1, 2, 3]  
list2 = [3, 2, 4] 
  
# using numpy.prod() to get the multiplications  
result1 = numpy.prod(list1) 
result2 = numpy.prod(list2) 
print(result1) 
print(result2) 
2
a_list = [1, 2, 3]

a_list = [item * 2 for item in a_list]

print(a_list)
OUTPUT
[2, 4, 6]
1
import numpy as np

list1 = [1,2,3,4,5]
result = np.prod(list1)

print(result)
>>>120
0

New to Communities?

Join the community