This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to multiply all the elements of the list together?

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

I would use R, which has a built in function for it, prod(1:10) returns 10 factorial. Is this homework? It's probably going to be closed as not bioinformatics, and you should ask the general programming forums. It's not clear what youre trying to do, since the multiplyList() appears to work!

Hello macadamopetrus!

We believe that this post does not fit the main topic of this site.

Pure programming, not primarily bioinformatics

For this reason we have closed your question. This allows us to keep the site focused on the topics that the community can help with.

If you disagree please tell us why in a reply below, we'll be happy to talk about it.

Cheers!

A hint:

You’re looking for the python function reduce, but this thread has been rightly closed. Google “python multiply elements of list”.

0 answers

No answers yet.

Log in to answer this question.