Menu
Coddy logo textTech

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.

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 items by their values
    # Hint: data_dict.items() returns (key, value) pairs
    # Hint: Use the 'key' parameter of sorted() to sort by value

    # Write code here

All lessons in Logic & Flow