How To Sum a List in Python

Python’s list data type is one of the most useful in the language. A list in Python is nothing more than an array of elements. Elements having string, integer, float, and other attributes can be found in lists.

Sum a List in Python

There are times when we must add numbers contained in a list in Python. This article will show you just that. 

Using the sum() function

Sum() is a function that allows you to add two or more numbers together. The sum () function is the most straightforward way to calculate the sum of all members in a list or tuple. In Python, the sum () function sees an iterable, be it a tuple, list, or a set as an argument, computes the sum of its members, and finally returns an integer value as the sum total.

Python’s sum function syntax:

Sum a List in Python

The sum() function returns the total number of items (e.g. members in a list) in an iterable as a number. Iterables can take the shape of lists, tuples, or dictionaries, but they must always be numbers. On the other hand, the optional “start” is added to the iterable’s total of numbers. The start is presumed to be 0 if it is not specified in the syntax.

The following is an example of using the built-in function sum() to add all the members in a list:

The output will be:

Below is an example of using a sum() function, by indicating the start

How to Sum a List in Python

The output will be:

Using lambda function

In Python, lambda functions are special functions that are available and used when the need arises. They’re anonymous functions, which means they don’t have a name.

Using the lambda function, we will find the sum of all entries in the list in the example below.

Using Lambda to sum list in python

The output of the code above will give:

Using zip() and list comprehension

A list comprehension is a syntactic construct that builds a list from a list that already exists. The syntax was influenced by the mathematical language of sets. Python’s syntax is based on the Haskell programming language.  

L = [expression for sequence variable [if condition]]

The pseudo-code above illustrates the syntax of a list comprehension. A list’s comprehension generates a new list. It’s based on a previous list. The series is traversed using a for loop. If the condition is met, an expression is evaluated for each loop. The value is inserted into the new list if it is computed. The “condition” is not required.

Assume we have a list [(1, 1), (2, 0), (0, 3)] and we want to add the first and second tuple values together to get the “summed tuple” (1+2+0, 1+0+3)= (3, 4). The code is as follows:

The output will be:

Using recursive approach

A great approach for calculating the sum of list members is by using the recursive approach. The list whose elements are to be added is the only input for this procedure.

It returns the sum of the first element and the result of its recursive call with the first element deleted from the list on each iteration. As a result, the size(or length) of the list received as an argument is reduced by one in each call.

This method checks the length of the list on every call. When the length approaches 1, the first element is returned. As a result, entries are added to the recursive call list with each call (starting from the first element till the last element).

The slicing operator is used to remove the initial element (:).

Expression num_list[1:] means all list elements starting from the element at index 1(second element) since the index of elements in a list starts from 0.

Below is an example:

Python sum list using recursive approach

This will produce the output below:

Finding the sum of two lists in python

In python, we can also find the sum of multiple lists. Here’s how:

Finding the sum of two lists in python

The output will be

We only needed three lists: list1, list2, and result. Then we added some elements to list1 and list2, and the result was set to an empty list. We iterated over the elements with a for loop and inserted corresponding elements.

How to sum a list in Python using for loop

A “for-loop” permits you to iterate over a set of items, with each iteration executing a single block of code.

The general syntax is as follows:

for in : else:

To put this into context, let’s use the for loop, to sum up all the elements in the OddNumbers list below:

The output will be:

Leave a Comment