Mapping Over Dictionary Values In Python

Mapping over dictionary values in Python

This article will give you with several methods of mapping over dictionary values in Python. Let’s read it now to get more knowledge.

Mapping over dictionary values in Python

Mapping over dictionary values in Python means iterating over the elements in the dictionary and mapping a function over all values in a dictionary, replacing each value with the result of applying the function to the value.

Using For Loop

Syntax:

for value in sequence:
    #loop body

Parameters:

  • value: represents the element of the sequence on each iteration.
  • sequence: an iterable datatype such as a list, a tuple, a set, or a string.

First, we define a function to take values from the elements of the dictionary. Next, we use a For Loop to iterate over the elements of the dictionary and put it to the function we defined earlier. To do that, use the items() function to convert the dictionary into a view object.

temperatures = {"US" : 15, "UK" : 14, "JP" : 28}

def c_to_f(cTemp):
  fTemp = (cTemp * 1.8) + 32 # 1f = 1c * 1.8 + 32
  return fTemp

fTemperatures = {}

# temperatures.items() = ([('US', 15), ('UK', 14), ('JP', 28)])
for country, temp in temperatures.items():
    fTemperatures[country] = c_to_f(temp) 

print(fTemperatures)

Output:

{'US': 59.0, 'UK': 57.2, 'JP': 82.4}

Using dictionary comprehension

Syntax:

dictionary = {key: value for element in iterable}

Parameters:

  • key: a key will be the key to the dictionary
  • value: a value will be the value of the dictionary
  • element: represent the element in each loop
  • iterable: Any Python object that permits it to be iterated, such as strings, lists, tuples, etc.

We can also use dictionary comprehension to shorten the code, like the example below:

temperatures = {"US" : 15, "UK" : 14, "JP" : 28}

def c_to_f(cTemp):
    fTemp = (cTemp * 1.8) + 32 # 1f = 1c * 1.8 + 32
    return fTemp

# temperatures.items() = ([('US', 15), ('UK', 14), ('JP', 28)])
# {country : c_to_f(temp)} is pushed into the dict at each loop 
fTemperatures = { country: c_to_f(temp) for country, temp in temperatures.items() }
print(fTemperatures)

Output:

{'US': 59.0, 'UK': 57.2, 'JP': 82.4}

Using valmap() function in Toolz package

Syntax:

valmap(function, dictionary)

Description:

Similar to map(), valmap() executes a function for each element in a dictionary.

The Toolz package provides utility functions for iterators and dictionaries. It can help us to use valmap() in the dictionary. So we can easily map over dictionary values in Python. To use valmap(), you must import Toolz into your application using this line of code:

import toolz

Consider the example below to better understand how to use the valmap() function.

import toolz

temperatures = {"US" : 15, "UK" : 14, "JP" : 28}

def c_to_f(cTemp):
    fTemp = (cTemp * 1.8) + 32 # 1f = 1c * 1.8 + 32
    return fTemp

# temperatures_value -> c_to_f(temperatures_value)
print(toolz.valmap(c_to_f, temperatures))

Output:

{'US': 59.0, 'UK': 57.2, 'JP': 82.4}

Summary

Through what you have read in this article, we hope you know mapping over dictionary values in Python. Please leave a comment if there’s anything you don’t understand in the article. We will respond as possible. Thank you for reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *