Mapped List Average

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In Python, we can use the map() function to apply a function to each element of a list and return a new list with the modified elements. We can also use the sum() function to add up all the elements of a list. In this challenge, we will combine these two functions to calculate the average of a list, after mapping each element to a value using a provided function.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/dictionaries("`Dictionaries`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/FunctionsGroup -.-> python/default_arguments("`Default Arguments`") python/FunctionsGroup -.-> python/lambda_functions("`Lambda Functions`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-13588{{"`Mapped List Average`"}} python/lists -.-> lab-13588{{"`Mapped List Average`"}} python/tuples -.-> lab-13588{{"`Mapped List Average`"}} python/dictionaries -.-> lab-13588{{"`Mapped List Average`"}} python/function_definition -.-> lab-13588{{"`Mapped List Average`"}} python/default_arguments -.-> lab-13588{{"`Mapped List Average`"}} python/lambda_functions -.-> lab-13588{{"`Mapped List Average`"}} python/build_in_functions -.-> lab-13588{{"`Mapped List Average`"}} end

Mapped List Average

Write a function called average_by(lst, fn = lambda x: x) that takes a list lst and a function fn as arguments. The function fn should be used to map each element of the list to a value. The function should then calculate the average of the mapped values and return it.

If the fn argument is not provided, the function should use the default identity function, which simply returns the element itself.

Your function should meet the following requirements:

  • 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).
  • Omit the last argument, fn, to use the default identity function.

Function signature: def average_by(lst, fn = lambda x: x) -> float:

def average_by(lst, fn = lambda x: x):
  return sum(map(fn, lst), 0.0) / len(lst)
average_by([{ 'n': 4 }, { 'n': 2 }, { 'n': 8 }, { 'n': 6 }], lambda x: x['n'])
## 5.0

Summary

In this challenge, we learned how to use the map() and sum() functions to calculate the average of a list, after mapping each element to a value using a provided function. We also learned how to use default arguments in Python functions.

Other Python Tutorials you may like