AppDividend
Latest Code Tutorials

Python Sum List: How to Find Sum of List in Python

0

If you have a list of numbers and you need the sum of the numbers, then use the Python inbuilt sum() function.

Python Sum List

To find the sum of the list in Python, use the sum() function. To add floating-point numbers with exact precision, use the fsum(iterable) instead. To concatenate elements of the given iterable (elements must be strings), you can use the join() method.

Syntax of sum()

sum(iterable, start)

Parameters

iterable: It is a required parameter that can be anything like list, tuples, or dictionaries, but most importantly, it should be numbered means it should be a number data type.
start: It is an optional parameter that can be added to the sum of numbers in the iterable. If the start is not given in the syntax, it is assumed to be 0.

Example

# app.py

numbers = [11, 21, 19, 18, 46]

# start parameter is not provided
sumofList = sum(numbers)
print(sumofList)

# start = 10
sumofList = sum(numbers, 10)
print(sumofList)

Output

115
125

In the first example, we defined the numbers list, and it sums up all the numbers in the list numbers and takes start to be 0, so returning only the sum of the numbers in the list.

In the second example, we sum up all the numbers in the list and takes start to be 10, so returning the sum of the numbers in the list + 10.

Pass data types other than numbers

If you pass a string or character to the sum() function, it will return an error.

# app.py

numbers = ['abc', 'def', 'ghi']

# start parameter is not provided
sumofList = sum(numbers)
print(sumofList)

Output

python3 app.py
Traceback (most recent call last):
  File "app.py", line 4, in <module>
    sumofList = sum(numbers)
TypeError: unsupported operand type(s) for +: 'int' and 'str'

We get the TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’.

Find the average of the list.

To find an average of the list, use the combination of sum() and len() function.

# app.py

numbers = [1, 10, 7]

# start parameter is not provided
sumofList = sum(numbers)

avgList = sumofList / len(numbers)
print(avgList)

Output

6.0

Computing the sum recursively.

You can calculate the sum of the list using recursion. Once the recursion is started and reaches the end, the function will start to roll back.

The sum_of_list() function takes two arguments as parameters: The list and the list(n). Initially, n is set at the maximum possible index in the list and decremented at each recursive call.

# app.py

numbers = [1, 10, 7]


def sum_of_list(l, n):
    if n == 0:
        return l[n]
    return l[n] + sum_of_list(l, n-1)


print("The sum of numbers is", sum_of_list(numbers, len(numbers)-1))

Output

The sum of numbers is 18

That is it for finding the sum of the list in Python.

See also

Python reduce()

Python mean

Python mode

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.