Search code examples
pythonlistaddition

python how to add all elements of a list together


if I had a list

results = ['1','4','73','92']

how do I get that list to call on itself and add all elements together? the elements have to be strings


Solution

  • One way to do this is using a list comprehension + sum operator:

    sum([float(num) for num in results])
    

    Note that it's safer to use float() instead of int() since your elements may include decimals.