Mapped List Average | Challenge

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/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ErrorandExceptionHandlingGroup(["`Error and Exception Handling`"]) 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/ErrorandExceptionHandlingGroup -.-> python/custom_exceptions("`Custom Exceptions`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/lists -.-> lab-13056{{"`Mapped List Average | Challenge`"}} python/tuples -.-> lab-13056{{"`Mapped List Average | Challenge`"}} python/dictionaries -.-> lab-13056{{"`Mapped List Average | Challenge`"}} python/function_definition -.-> lab-13056{{"`Mapped List Average | Challenge`"}} python/default_arguments -.-> lab-13056{{"`Mapped List Average | Challenge`"}} python/lambda_functions -.-> lab-13056{{"`Mapped List Average | Challenge`"}} python/custom_exceptions -.-> lab-13056{{"`Mapped List Average | Challenge`"}} python/build_in_functions -.-> lab-13056{{"`Mapped List Average | Challenge`"}} end

Mapped List Average

Problem

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:

Example

assert average_by([1, 2, 3, 4, 5]) == 3.0
assert average_by([1, 2, 3, 4, 5], lambda x: x**2) == 11.0
assert 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