-5
mynumbers=[0,1,0,1,6,0,2,0,9]
def sum(mynumbers):
    total = 0
    for x in mynumbers:
        total =1+x
    return total
print(sum(mynumbers)) 

I do like that but I want to find the sum in list type

how I find sum of number in list ?

vaultah
  • 44,105
  • 12
  • 114
  • 143
  • please explain better what you are trying to do. to sum the list simply do: sum(number) – Vico Oct 24 '17 at 20:32
  • I find 19 but I want to find [19] – Bayram Tora Oct 24 '17 at 20:33
  • you want your answer to be a list? [sum(number)] – Vico Oct 24 '17 at 20:35
  • yeah ı know do it like this but I need different solutions – Bayram Tora Oct 24 '17 at 20:36
  • this link might help: https://stackoverflow.com/questions/4362586/sum-a-list-of-numbers-in-python – Vico Oct 24 '17 at 20:37
  • Python has a built-in function named `sum` that computes the sum of a list very efficiently. Why do you want to write your own list summing function? – PM 2Ring Oct 24 '17 at 20:41
  • 1
    @BayramTora Python has a built-in function called sum, you don't need to define it yourself. If you want the answer to be in a list, and you want to use the sum function, you can either write `[sum(mynumbers)]` or `list(sum(mynumbers))`. Or, you can use the sum function you have defined and return the total inside a list like `return [total]`, but in my opinion you should rename your function as well then. – user_ Oct 24 '17 at 20:41
  • If you have a different question, e.g. about cumulative sum, post it as a new question. – vaultah Oct 24 '17 at 21:33

1 Answers1

1

I'm not sure what you are wanting here, but given your current function, do this:

>>> myNumbers = [0,1,0,1,6,0,2,0,9]
>>> sum(myNumbers)
19

The built-in sum() method works on an iterable already.

pstatix
  • 3,611
  • 4
  • 18
  • 40