Multiply all numbers in the list with python

Configurare noua (How To)

Situatie

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

Input : list1 = [3, 2, 4] 
Output : 24

Backup

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))

Output:

6
24

Solutie

Method2
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)
Output: 

6
24

Tip solutie

Permanent

Voteaza

(10 din 16 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?