• Register
1 vote
56 views

Problem:

Recently I started learning python and just about to know more about list and tuple in python. I am a little bit known to the list and tuple and I can add or subtracts elements in a list. I wonder how to multiply all elements in a list python? I tried it in the adding process but it didn’t work for me. Is it doable?
Could anybody please show me the way to do it?

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

1 vote
 
Best answer

Solution:

Yes! Multiplication of all elements in a list of python is doable. You can multiply the elements in a couple of ways. I am trying to make it easier that you can understand the program real quick.

Method 1: Traversal

def multiList(numList) :       
    value = 1
    for n in numList: 
         value = value * n  
    return value        
list1 = [1, 2, 3]  
list2 = [3, 4, 5]

print(multiList(list1)) 
print(multiList(list2)) 

Method 2: Using numpy.prod()

import numpy  
list1 = [1, 2, 3]  
list2 = [3, 4, 5] 
  
value1 = numpy.prod(list1) 
value2 = numpy.prod(list2) 
print(value1) 
print(value2) 

Both of the case will produce an output:

6
60

Thanks

13 9 6
94,260 points

Related questions

1 vote
1 answer 14 views
14 views
Problem: Any clue how to slove this calculation? Multiply all values in a list python.
asked Mar 26 PkGuy 23.5k points
0 votes
1 answer 51 views
51 views
Problem: How to I Solve this : can't multiply sequence by non-int of type 'list' HELP! I need help understanding this: can't multiply sequence by non-int of type 'list' Can someone please help solve this, I am new in python().
asked Mar 2 Mashhoodch 13k points
0 votes
1 answer 4 views
4 views
Problem: Python number of elements in a list.
asked Apr 11 Dan phillip 4.8k points
0 votes
1 answer 9 views
9 views
Problem: Please solve it … Because I am unable to find out the solution .. How to add elements to list in python
asked Apr 27 Ifra 34.7k points
0 votes
1 answer 9 views
9 views
Problem: How can I list all the files in a directory in python and add them to a list?
asked Mar 17 sasha 16.2k points
0 votes
1 answer 75 views
75 views
Problem : How to remove all occurrences of a value from a list? I tried in Python remove() will remove the first occurrence of value in a list. How to remove all occurences?
asked Oct 22, 2019 peterlaw 6.9k points
0 votes
1 answer 6 views
6 views
Problem: How to remove all occurrences of a value from a list?
asked Apr 16 sumaiya simi 43.9k points
0 votes
2 answers 49 views
49 views
Problem: I am a newbee in python programming. I tried to remove all occurrences of a character in a list python. So I wrote a code: Nums = [11, 22, 11, 33, 11, 44, 11, 55] a = 11 print ("Original Numbers:") print (Nums) i=0 length = len(Nums) while(i< ... line 8, in <module> if(Nums[i]==a): IndexError: list index out of range Why I am getting this output but not the actual result. Please help
asked Mar 22, 2020 LizzyM 6.1k points
0 votes
1 answer 169 views
169 views
Problem : Facing following issue related to list in Python given that a refers to a list, write the necessary code to reverse the elements of the list.
asked Nov 16, 2019 peterlaw 6.9k points
1 vote
1 answer 32 views
32 views
Problem: I was trying to find common elements in two lists python. So I wrote this code: x = [1, 2, 6, 9] y = [1, 3, 8, 9] x_as_set = set(y) intersection =x_as_set.intersection(y) intersection_as_list = list(intersection) print(intersection_as_list) But when I compile this it gives error. Please help me to find common elements in two lists python
asked Mar 29, 2020 LizzyM 6.1k points