I
I
igoodmood2015-11-19 19:12:35
Python
igoodmood, 2015-11-19 19:12:35

How to find the sum of all input elements of a list?

How to find the sum of any number of entered list items in python? (meaning numerical values).

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Alexander, 2015-11-19
@igoodmood

One solution is to use the for statement

spisok = [1,2,3,4,5]
summa=0
for el in spisok:
    summa += el

D
Dmitry Demidov, 2015-11-19
@ptitca_zu

sum

E
Evgeniy _, 2015-11-19
@GeneD88

lst = [1,2,3,4,5,"string"]
sum([i for i in lst if str(i).isdigit()])
>>> 15

A
angru, 2015-11-20
@angru

as an option, if according to the conditions of the problem it is impossible to use sum
Python 2:
Python 3:

from functools import reduce
from operator import add

x = reduce(add, [1, 2, 3, 4, 5])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question