Off topic:How to multiply all the elements of the list together?
0
0
Entering edit mode
5.3 years ago

Dear all, I wrote a code to multiply all the elements from the lists. It goes something like this:

for i in list(combinations):
    new_value = list()
     result = 1
     for answers in list(combinations):
          result = result * answers
    return result
 print(new_value)

But the problem is that this function doesn't work. But what does work is:

def multiplyList(myList):

    result = 1
    for z in myList:
          result = result * z
    return result


combinations = [4, 2, 2, 6]
print(multiplyList(combinations))

96

This gives the result as expected 96. But then the problem is that I have to copy past the list of my combinations in the code and get the answer. Does anyone know how to automatically read the list and multiply all the numbers in the combination list?

python for loops • 1.9k views
ADD COMMENT
This thread is not open. No new answers may be added
Traffic: 1697 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6