Reputation: 21
I used this here to sum the second value of each tuple in a list: https://stackoverflow.com/a/12218119/9195816
sum(n for _, n in structure)
works fine. But i dont need the sum, i only need the average. So something like sum(n for _, n in structure) \ total_amount_of_values
. But of course, this won't work:
TypeError: unsupported operand type(s) for /: 'float' and 'list'
My list looks i.e. like this: [1000, 900.84, 500, 1240.11]
Upvotes: 0
Views: 831
Reputation: 7210
n = [1000, 900.84, 500, 1240.11]
average = sum(n)/len(n)
This will give you the average of the list n
But it sounds like your list looks more like this
n = [(a,b), (c,d), ...]
and you want
b + d + ... / len(n)
If this is the case, then you can do this like so
average = sum(map(lambda x: x[1], n)) / len(n)
Upvotes: 1