Sorting Dictionaries in Python: Keys, Values, and More

Youโ€™ve got a dictionary, but youโ€™d like to sort the key-value pairs. Perhaps youโ€™ve tried passing a dictionary to the sorted() function but didnโ€™t receive the results you expected. In this video course, youโ€™ll go over everything you need to know to sort dictionaries in Python.

In this video course, youโ€™ll:

  • Review how to use the sorted() function
  • Learn how to get dictionary views to iterate over
  • Understand how dictionaries are cast to lists during sorting
  • Learn how to specify a sort key to sort a dictionary by value, key, or nested attribute
  • Review dictionary comprehensions and the dict() constructor to rebuild your dictionaries
  • Consider alternative data structures for your key-value data

Whatโ€™s Included:

Downloadable Resources:

About Stephen Gruppetta

Stephen worked as a research physicist in the past, developing imaging systems to detect eye disease. He now focuses on Python education!

ยป More about Stephen

Each tutorial at Real Python is created by a team of developers so that it meets our high quality standards. The team members who worked on this tutorial are:

โ† Browse All Courses