Python sum second element in list of tuples

I have structure like this:

structure = [('a', 1), ('b', 3), ('c', 2)]

I would like to sum the integers (1+3+2) using sum() builtin method (in one line).

Any ideas?

Python sum second element in list of tuples

asked Aug 31, 2012 at 15:10

Python sum second element in list of tuples

sum(n for _, n in structure)

would work.

answered Aug 31, 2012 at 15:11

Python sum second element in list of tuples

David RobinsonDavid Robinson

75.5k15 gold badges162 silver badges180 bronze badges

13

sum(x[1] for x in structure)

should work

answered Aug 31, 2012 at 15:11

Python sum second element in list of tuples

mgilsonmgilson

288k60 gold badges601 silver badges675 bronze badges

You could do

sum(zip(*structure)[1])

nbro

14.3k27 gold badges104 silver badges188 bronze badges

answered Aug 31, 2012 at 15:13

EricEric

92.6k52 gold badges230 silver badges360 bronze badges

2

Using a functional style, you could do

reduce(lambda x,y:x+y[1], structure,0)

nbro

14.3k27 gold badges104 silver badges188 bronze badges

answered Aug 31, 2012 at 15:26

John WangJohn Wang

4,4548 gold badges34 silver badges50 bronze badges

1

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

Use a generator expression to sum the second element of each tuple in a list, e.g. result = sum(tup[1] for tup in list_of_tuples). The sum() function gets passed a generator object with the second element of each tuple in the list and returns the total.

Copied!

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.

Copied!

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.

Copied!

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.

Copied!

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.

Which approach you pick is a matter of personal preference. I'd use directly index access as if I find it easier to read and more explicit.

How do you sum two tuples in Python?

To add two tuples element-wise: Use the zip function to get an iterable 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.

How do you find the sum of the tuples in a list?

To find a sum of the tuple in Python, use the sum() method. Define a tuple with number values and pass the tuple as a parameter to the sum() function; in return, you will get the sum of tuple items.

Can you sum tuples in Python?

The built-in sum() function in Python is used to return the sum of an iterable. To get the sum total of a tuple of numbers, you can pass the tuple as an argument to the sum() function.

How do you sum items in a list in Python?

Python provides an inbuilt function sum() which sums up the numbers in the list. Syntax: sum(iterable, start) iterable : iterable can be anything list , tuples or dictionaries , but most importantly it should be numbers. start : this start is added to the sum of numbers in the iterable.