Map Dictionary Values

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-13685{{"`Map Dictionary Values`"}} python/variables_data_types -.-> lab-13685{{"`Map Dictionary Values`"}} python/for_loops -.-> lab-13685{{"`Map Dictionary Values`"}} python/lists -.-> lab-13685{{"`Map Dictionary Values`"}} python/tuples -.-> lab-13685{{"`Map Dictionary Values`"}} python/dictionaries -.-> lab-13685{{"`Map Dictionary Values`"}} python/function_definition -.-> lab-13685{{"`Map Dictionary Values`"}} python/lambda_functions -.-> lab-13685{{"`Map Dictionary Values`"}} python/data_collections -.-> lab-13685{{"`Map Dictionary Values`"}} python/build_in_functions -.-> lab-13685{{"`Map Dictionary Values`"}} end

Map Dictionary Values

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.

def map_values(obj, fn):
  return dict((k, fn(v)) for k, v in obj.items())
users = {
  'fred': { 'user': 'fred', 'age': 40 },
  'pebbles': { 'user': 'pebbles', 'age': 1 }
}
map_values(users, lambda u : u['age']) ## {'fred': 40, 'pebbles': 1}

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