[('1950', '6.5', '6.4', '6.3', '5.8', '5.5', '5.4', '5.0', '4.5', '4.4', '4.2',
'4.2', '4.3', ' \n')]
How can I sum up the values from position [1] to [12]?
[('1950', '6.5', '6.4', '6.3', '5.8', '5.5', '5.4', '5.0', '4.5', '4.4', '4.2',
'4.2', '4.3', ' \n')]
How can I sum up the values from position [1] to [12]?
Convert to float and sum using a list comprehension:
sum(float(x) for x in my_list[0][1:13])
edit: eeks, range was wrong. edited per comments.
You could use a lambda and reduce.
lst = [('1950', '6.5', '6.4', '6.3', '5.8', '5.5', '5.4', '5.0', '4.5', '4.4', '4.2', '4.2', '4.3', ' \n')]
lst_sum = reduce(lambda x, y : float(x) + float(y), lst[0][1:12])
sum as a variable name as it clashes with the builtin function sum - which coincidentally does the same job as the reduce+lambda. I think the OP expects the items to be added as floats, not as strings