• Register
1 vote
45 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,240 points

Related questions

0 votes
1 answer 66 views
66 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
2 answers 39 views
39 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
2 answers 12 views
12 views
Problem: How a list is the most flexible data structure in Python. Whereas, a 2D list which is commonly known as a list of lists, is a list object where every item is a list itself - for example: [[1,2,3], [4,5,6], [7,8,9]].
asked Jan 26 Mashhoodch 9.9k points
1 vote
1 answer 52 views
52 views
Problem: I was trying to check if an element does exist in a list using a pyhton program: primary_list = [ 10, 12, 16, 6, 20, 33 ] a = input("Please enter a number to check if this exists in the list: ") print("Checking if input exists in this list : ") for ... not giving any output that is right. What should I do to fix this and how to check if an element does exist in a list. Thanks in advance.
asked May 16, 2020 samhaz 5k points
1 vote
1 answer 45 views
45 views
Problem: I have 2 list that consist of some name and some numbers: list_1 = ['samhaz', 'porto', 'chris', 'evans'] list_2 = ['33', '14', '26', '30'] I tried to add this two list together(the names with the numbers). For that I wrote a python program: ... ' object has no attribute 'add' What is the problem in my program and how can I add those 2 list(names and numbers) together. Thanks in advance.
asked May 13, 2020 samhaz 5k points
1 vote
1 answer 27 views
27 views
Problem: I have a simple and basic level question to you guy. How can I replace an element in a list python? Yeah, it might be a silly question but need to be answered badly. I am struggling with this problem for the last thirty minutes almost. But I am unable to sort out this thing. Your kind help is much needed. Thanks
asked Apr 4, 2020 Gavin 15.3k points
1 vote
1 answer 24 views
24 views
Problem: Hello Kodlogers, Myself is a high school student. I&rsquo;ve started learning Python programming recently. I am wanting to replace an item in a list python. Let&rsquo;s say I have a list of integers like: my_list = [10, -15, 1, 8, 0, 9, -5, 13, -1, 5] ... or 1. Negatives values with -1, positive values with 1 and leave 0 untouched. Is it doable? If doable, how? Thanks for your help, mate.
asked Apr 4, 2020 Gavin 15.3k points
0 votes
1 answer 130 views
130 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
0 votes
1 answer 7 views
7 views
Problem: HI KODLOGS: I want to understand how to check if list is eampty in python please explain in detail with examples. For example, if passed the following: a = [] How do I check to see if a is empty?
asked Jan 26 Mashhoodch 9.9k points
0 votes
2 answers 483 views
483 views
Problem : Python Program to Calculate the Average of Numbers in a Given List
asked Oct 21, 2019 peterlaw 6.9k points