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

avatar
Borislav Hadzhiev

Last updated: Feb 19, 2023
6 min

banner

# Table of Contents

  1. Sum the elements of a Tuple in Python
  2. Sum the elements of a Tuple using a for loop
  3. Sum the elements of each Tuple in a List of Tuples
  4. Sum a List of Tuples element-wise in Python
  5. Sum two Tuples element-wise in Python
  6. Sum the second element of each tuple in a list in Python

# Sum the elements of a tuple 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

sum the elements in a tuple

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:

NameDescription
iterablethe iterable whose items to sum
startsums the start value and the items of the iterable. sum defaults to 0 (optional)

# Sum the elements of a Tuple using a for loop

You 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

sum elements of tuple using for loop

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.

# Sum the elements of each tuple in a list of tuples

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]

sum elements of each tuple in list of tuples

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.

# Sum a list of tuples element-wise in Python

To sum a list of tuples element-wise:

  1. Use the zip function to get an iterator of tuples with the corresponding items.
  2. Use a list comprehension to iterate over the iterable.
  3. 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 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]

# Sum two Tuples element-wise in Python

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 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)

# Sum the second element of each tuple in a list in Python

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

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.

# Additional Resources

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

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev