I'm trying to find the average of the values inside of the tuples in this list
[('coinbasepro', 10822.0), ('bitstamp', 10832.82)]
What would be the most pythonic way (like lambdas, reduce, list comprehensions) to do this simple operation?
I'm trying to find the average of the values inside of the tuples in this list
[('coinbasepro', 10822.0), ('bitstamp', 10832.82)]
What would be the most pythonic way (like lambdas, reduce, list comprehensions) to do this simple operation?
import numpy as np
data = [('coinbasepro', 10822.0), ('bitstamp', 10832.82)]
average = np.mean([i[1] for i in data])
Here is one way that doesn't not require any extra packages (assuming your list will always be in this format):
l1 = [('coinbasepro', 10822.0), ('bitstamp', 10832.82)]
nums = [x[1] for x in l1]
avg = sum(nums) / len(nums)
print(avg)
# 10827.41