R
R
Ruslan Amerkhanov2018-03-08 13:21:42
Python
Ruslan Amerkhanov, 2018-03-08 13:21:42

How to more concisely solve the following problem?

Given a list consisting of lists, each of which includes two elements - a string and the result of a calculation. You need to get the sum of all second elements. Below is my solution code. I think there are better solutions. Tell me please.

a = ['asd', 1 / 5]
c = ['asda', 1 / 6]
b = [a, c]
summ = 0
for i in range(0, len(b)):
    summ += b[i][1]
print(summ)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
aRegius, 2018-03-08
@nice-coding

sum(i[1] for i in b)

S
Sergey Gornostaev, 2018-03-08
@sergey-gornostaev

sum(map(lambda x: x[1], b))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question