Sum the elements of a Tuple or List of Tuples in Python

Borislav Hadzhiev

Last updated: Apr 9, 2024

Reading timeยท6 min

- Sum the elements of a Tuple in Python
- Sum the elements of a Tuple using a for loop
- Sum the elements of each Tuple in a List of Tuples
- Sum a List of Tuples element-wise in Python
- Sum two Tuples element-wise in Python
- Sum the second element of each tuple in a list in Python

**Use the sum() function to sum the elements of a tuple.**

**The sum function takes an iterable (such as a tuple) as an argument, sums
its items from left to right and returns the total.**

main.py

`my_tuple = (10, 20, 30) result = sum(my_tuple) print(result) # ๐๏ธ 60`

The code for this article is available on GitHub

We used the `sum()`

function to sum the elements of a tuple.

main.py

`my_tuple = (10, 20, 30) result = sum(my_tuple) print(result) # ๐๏ธ 60`

The sum() function takes an iterable, sums its items from left to right and returns the total.

The function takes the following 2 arguments:

Name | Description |
---|---|

iterable | the iterable whose items to sum |

start | sums the `start` value and the items of the iterable. `sum` defaults to `0` (optional) |

`for`

loopYou can also use a for loop to sum up the elements of a tuple.

main.py

`my_tuple = (10, 20, 30) total = 0 for element in my_tuple: total += element print(total) # ๐๏ธ 60`

The code for this article is available on GitHub

We declared a new variable and initialized it to `0`

.

On each iteration of the `for`

loop, we add the current element to the `total`

variable.

If you need to sum the elements of each tuple in a list, use a list comprehension.

main.py

`# โ sum the elements of each tuple in a list of tuples list_of_tuples = [(10, 20), (30, 40), (50, 60)] result = [sum(t) for t in list_of_tuples] print(result) # ๐๏ธ [30, 70, 110]`

List comprehensions are used to perform some operation for every element, or select a subset of elements that meet a condition.

In the example, we iterate over the list and pass each tuple to the `sum()`

function to sum its elements.

The first tuple has a sum of `30`

, the second a sum of `70`

, and the third a sum
of `110`

.

To sum a list of tuples element-wise:

- Use the
`zip`

function to get an iterator of tuples with the corresponding items. - Use a list comprehension to iterate over the iterable.
- On each iteration, pass the tuple to the
`sum()`

function.

main.py

`list_of_tuples = [(1, 2), (3, 4), (5, 6)] # ๐๏ธ [(1, 3, 5), (2, 4, 6)] print(list(zip(*list_of_tuples))) result = [sum(tup) for tup in zip(*list_of_tuples)] print(result) # ๐๏ธ [9, 12]`

The code for this article is available on GitHub

The zip() function iterates over several iterables in parallel and produces tuples with an item from each iterable.

main.py

`list_of_tuples = [(1, 2), (3, 4), (5, 6)] # ๐๏ธ [(1, 3, 5), (2, 4, 6)] print(list(zip(*list_of_tuples)))`

We used the `*`

iterable unpacking operator to unpack the tuples in the call to
the `zip()`

function.

main.py

`list_of_tuples = [(1, 2), (3, 4), (5, 6)] # ๐๏ธ (1, 2) (3, 4) (5, 6) print(*list_of_tuples)`

The * iterable unpacking operator enables us to unpack an iterable in function calls, in comprehensions and in generator expressions.

You can imagine that the `zip()`

function iterates over the tuples, taking 1
item from each.

main.py

`list_of_tuples = [(1, 2), (3, 4), (5, 6)] # ๐๏ธ [(1, 3, 5), (2, 4, 6)] print(list(zip(*list_of_tuples)))`

The first tuple in the list consists of the elements in each tuple that have an index of

`0`

, and the second tuple consists of the elements in each tuple that have an index of `1`

.The last step is to use a list comprehension to iterate over the `zip`

object
and sum each tuple.

main.py

`list_of_tuples = [(1, 2), (3, 4), (5, 6)] # ๐๏ธ [(1, 3, 5), (2, 4, 6)] print(list(zip(*list_of_tuples))) result = [sum(tup) for tup in zip(*list_of_tuples)] print(result) # ๐๏ธ [9, 12]`

List comprehensions are used to perform some operation for every element, or select a subset of elements that meet a condition.

The sum function takes an iterable, sums its items from left to right and returns the total.

On each iteration, we pass the current tuple to the `sum()`

function and get the
total.

You can use this approach with a list that stores tuples of arbitrary length.

main.py

`list_of_tuples = [(1, 2, 3), (4, 5, 6), (7, 8, 9)] # ๐๏ธ [(1, 4, 7), (2, 5, 8), (3, 6, 9)] print(list(zip(*list_of_tuples))) result = [sum(tup) for tup in zip(*list_of_tuples)] print(result) # ๐๏ธ [12, 15, 18]`

Use the `map()`

and `zip()`

functions to sum two or more tuples element-wise.

main.py

`tuple_1 = (1, 2, 3) tuple_2 = (4, 5, 6) result = tuple(map(sum, zip(tuple_1, tuple_2))) print(result) # ๐๏ธ (5, 7, 9)`

The code for this article is available on GitHub

The zip() function iterates over several iterables in parallel and produces tuples with an item from each iterable.

The `zip`

function returns an iterator of tuples.

main.py

`tuple_1 = (1, 2, 3) tuple_2 = (4, 5, 6) # ๐๏ธ [(1, 4), (2, 5), (3, 6)] print(list(zip(tuple_1, tuple_2)))`

The map() function takes a function and an iterable as arguments and calls the function with each item of the iterable.

The `map()`

function calls the `sum()`

function with an item from each tuple.

main.py

`tuple_1 = (1, 2, 3) tuple_2 = (4, 5, 6) result = tuple(map(sum, zip(tuple_1, tuple_2))) print(result) # ๐๏ธ (5, 7, 9)`

The last step is to convert the result to a tuple using the tuple() class.

This approach can be used to sum more than two tuples element-wise.

main.py

`tuple_1 = (1, 2, 3) tuple_2 = (4, 5, 6) tuple_3 = (7, 8, 9) result = tuple(map(sum, zip(tuple_1, tuple_2, tuple_3))) print(result) # ๐๏ธ (12, 15, 18)`

If you need to sum the second element of each tuple in a list, use a generator expression.

main.py

`list_of_tuples = [(10, 20), (30, 40), (50, 60)] result = sum(tup[1] for tup in list_of_tuples) print(result) # ๐๏ธ 120`

The first step is to use a generator expression to iterate over the list of tuples.

Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we access the tuple element at index `1`

(the second tuple
item) and return the result.

The example passes a generator object that contains the numbers `20`

, `40`

, `60`

to the `sum()`

function, which then returns `120`

.

You can use this approach to sum the Nth element of each tuple in a list.

Here is an example that sums the first element of each tuple in a list.

main.py

`list_of_tuples = [(10, 20), (30, 40), (50, 60)] result = sum(tup[0] for tup in list_of_tuples) print(result) # ๐๏ธ 90`

Python indexes are zero-based. The first item in a tuple (or any other iterable) has an index of

`0`

, the second an index of `1`

, etc.An alternative approach is to unpack the second item from each tuple in the generator expression.

main.py

`list_of_tuples = [(10, 20), (30, 40), (50, 60)] result = sum(second for _, second in list_of_tuples) print(result) # ๐๏ธ 120`

The code for this article is available on GitHub

We only assigned the second item in the tuples to a variable.

The first item is stored in an underscore because it's not needed.

We basically unpack the second item from the tuple of the current iteration and assign the value to a variable.

main.py

`first, second = (10, 20) print(first) # ๐๏ธ 10 print(second) # ๐๏ธ 20`

When using this approach, you have to make sure to declare exactly as many variables as you have items in the tuple.

I've also written an article on how to sum all values in a dictionary or a list of dictionaries.

You can learn more about the related topics by checking out the following tutorials: