Menu
CoddyTech

Recap - Dictionary Sorter

Part of the Logic & Flow section of Coddy's Python journey. Lesson 49 of 78.

challenge icon

Challenge

Medium

Create a function named dictionary_sorter that takes a dictionary data_dict as an argument. The function should sort the dictionary by its values in ascending order and return a new dictionary containing the sorted key-value pairs.

Tip: To sort the dictionary by its values, use the sorted() function with data_dict.get as the key argument. It returns the value for each key.

For example, if the input dictionary is {'a': 3, 'b': 1, 'c': 2}, the function should return a dictionary like this:

{'b': 1, 'c': 2, 'a': 3}

Try it yourself

def dictionary_sorter(data_dict):
    # Use sorted() to sort the dictionary keys by their values
    # Hint: data_dict.get(key) returns the value stored under key
    # Hint: Use the 'key' parameter of sorted() to sort by value

    # Write code here

All lessons in Logic & Flow

Practice on your own: Online Python compiler