30 seconds of python

  Back to Home

average_by

mathlistfunctionintermediate

Returns the average of a list, after mapping each element to a value using the provided function.

Use map() to map each element to the value returned by fn. Use sum() to sum all of the mapped values, divide by len(lst).

def average_by(lst, fn=lambda x: x):
  return sum(map(fn, lst), 0.0) / len(lst)