Map List to Dictionary

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 create a dictionary from a list where the keys are the elements of the list and the values are the result of applying a function to those elements. In this challenge, you will create a function that maps the values of a list to a dictionary using a 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(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") 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-13684{{"`Map List to Dictionary`"}} python/variables_data_types -.-> lab-13684{{"`Map List to Dictionary`"}} python/lists -.-> lab-13684{{"`Map List to Dictionary`"}} python/tuples -.-> lab-13684{{"`Map List to Dictionary`"}} python/dictionaries -.-> lab-13684{{"`Map List to Dictionary`"}} python/function_definition -.-> lab-13684{{"`Map List to Dictionary`"}} python/lambda_functions -.-> lab-13684{{"`Map List to Dictionary`"}} python/data_collections -.-> lab-13684{{"`Map List to Dictionary`"}} python/build_in_functions -.-> lab-13684{{"`Map List to Dictionary`"}} end

Map List to Dictionary

Write a Python function called map_dictionary(itr, fn) that takes two parameters:

  • itr: a list of values
  • fn: a function that takes a value as input and returns a value as output

The function should return a dictionary where the key-value pairs consist of the original value as the key and the result of the function as the value.

To solve this problem, follow these steps:

  1. Use map() to apply fn to each value of the list.
  2. Use zip() to pair original values to the values produced by fn.
  3. Use dict() to return an appropriate dictionary.
def map_dictionary(itr, fn):
  return dict(zip(itr, map(fn, itr)))
map_dictionary([1, 2, 3], lambda x: x * x) ## { 1: 1, 2: 4, 3: 9 }

Summary

In this challenge, you learned how to create a dictionary from a list where the keys are the elements of the list and the values are the result of applying a function to those elements. You used the map(), zip(), and dict() functions to solve the problem.

Other Python Tutorials you may like