Map Dictionary Values | Challenge

PythonPythonBeginner
Practice Now

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

Introduction

In Python, a dictionary is a collection of key-value pairs. Sometimes, we need to transform the values of a dictionary using a function. This challenge will test your ability to create a function that takes a dictionary and a function as arguments and returns a new dictionary with the same keys as the original dictionary and values generated by running the provided function for each value.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") 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/lambda_functions("`Lambda Functions`") python/PythonStandardLibraryGroup -.-> python/data_collections("`Data Collections`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-13153{{"`Map Dictionary Values | Challenge`"}} python/variables_data_types -.-> lab-13153{{"`Map Dictionary Values | Challenge`"}} python/for_loops -.-> lab-13153{{"`Map Dictionary Values | Challenge`"}} python/lists -.-> lab-13153{{"`Map Dictionary Values | Challenge`"}} python/tuples -.-> lab-13153{{"`Map Dictionary Values | Challenge`"}} python/dictionaries -.-> lab-13153{{"`Map Dictionary Values | Challenge`"}} python/function_definition -.-> lab-13153{{"`Map Dictionary Values | Challenge`"}} python/lambda_functions -.-> lab-13153{{"`Map Dictionary Values | Challenge`"}} python/data_collections -.-> lab-13153{{"`Map Dictionary Values | Challenge`"}} python/build_in_functions -.-> lab-13153{{"`Map Dictionary Values | Challenge`"}} end

Map Dictionary Values

Problem

Write a function map_values(obj, fn) that takes a dictionary obj and a function fn as arguments and returns a new dictionary with the same keys as the original dictionary and values generated by running the provided function for each value.

Example

users = {
  'fred': { 'user': 'fred', 'age': 40 },
  'pebbles': { 'user': 'pebbles', 'age': 1 }
}
map_values(users, lambda u : u['age']) ## {'fred': 40, 'pebbles': 1}

Constraints

  • The function should work for any dictionary and function that meet the requirements.
  • The function should not modify the original dictionary.

Summary

In this challenge, you learned how to create a function that takes a dictionary and a function as arguments and returns a new dictionary with the same keys as the original dictionary and values generated by running the provided function for each value. This is a useful technique for transforming the values of a dictionary.

Other Python Tutorials you may like