Sort Dictionary by Key

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

Dictionaries are an essential data structure in Python. They are used to store data in key-value pairs. However, sometimes it is necessary to sort the dictionary by key. In this challenge, you will be tasked with writing a function that sorts a dictionary by key.


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/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/dictionaries("`Dictionaries`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/FunctionsGroup -.-> python/default_arguments("`Default Arguments`") python/PythonStandardLibraryGroup -.-> python/data_collections("`Data Collections`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-13719{{"`Sort Dictionary by Key`"}} python/variables_data_types -.-> lab-13719{{"`Sort Dictionary by Key`"}} python/booleans -.-> lab-13719{{"`Sort Dictionary by Key`"}} python/tuples -.-> lab-13719{{"`Sort Dictionary by Key`"}} python/dictionaries -.-> lab-13719{{"`Sort Dictionary by Key`"}} python/function_definition -.-> lab-13719{{"`Sort Dictionary by Key`"}} python/default_arguments -.-> lab-13719{{"`Sort Dictionary by Key`"}} python/data_collections -.-> lab-13719{{"`Sort Dictionary by Key`"}} python/build_in_functions -.-> lab-13719{{"`Sort Dictionary by Key`"}} end

Sort Dictionary by Key

Write a function sort_dict_by_key(d, reverse=False) that takes a dictionary d and returns a new dictionary sorted by key. The function should have an optional parameter reverse that defaults to False. If reverse is True, the dictionary should be sorted in reverse order.

def sort_dict_by_key(d, reverse = False):
  return dict(sorted(d.items(), reverse = reverse))
d = {'one': 1, 'three': 3, 'five': 5, 'two': 2, 'four': 4}
sort_dict_by_key(d) ## {'five': 5, 'four': 4, 'one': 1, 'three': 3, 'two': 2}
sort_dict_by_key(d, True)
## {'two': 2, 'three': 3, 'one': 1, 'four': 4, 'five': 5}

Summary

In this challenge, you were tasked with writing a function that sorts a dictionary by key. You learned how to use the sorted() function to sort a list of tuple pairs from the dictionary and convert it back to a dictionary using the dict() function. You also learned how to use the reverse parameter in sorted() to sort the dictionary in reverse order.

Other Python Tutorials you may like