Loading video player…

Sorting Dictionaries in Python: Keys, Values, and More (Overview)

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 haven’t gotten the results you expected. In this video course, you’ll go over everything you need to know if you want 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
Download

Course Slides (.pdf)

1.8 MB
Download

Sample Code (.zip)

2.5 KB

00:00 Welcome to this Real Python video course, Sorting a Python Dictionary My name is Stephen, and I’ll be guiding you through this topic over the coming lessons.

00:09 Python’s dictionary is one of the main data types, which you learn about early on in your Python journey. But if you’ve ever tried sorting a Python dictionary, you’d have realized that it’s not as straightforward as when you’re dealing with other data types such as sequences like lists or tuples.

00:27 Let’s have a look at this dictionary, which contains points assigned to players who are playing some game. There are four elements in this dictionary. The keys are strings with the names of the players, and the values are integers with the number of points that each player has at any point in the game.

00:46 Now, if you want to sort this dictionary, the first question you need to ask is what does it mean, sorting this dictionary? For example, do you want to sort the dictionary based on the number of points that each player has?

01:01 In that case, you’d be sorting using values, and let’s say you want to go from highest to lowest. You’d have Jim in first place with 12 points, followed by Jill with 10, Jack with eight points and finally, Jane with five points.

01:15 Or perhaps you want to sort the dictionary using the player’s names and sort them out in alphabetical order. In this case, you’d be using the keys and you’d have Jack in first place, followed by Jane and then Jill and Jim. You may even have more complex use cases,

01:33 possibly with dictionaries, with nested data structures, and in those cases, you may want to define your own rules for how to sort dictionaries. In this course, you’ll be exploring all of these scenarios and learning how you can sort dictionaries whatever your application.

01:52 So let’s see what’s coming up in this course. The first part of this course will set the scene for what you need in order to sort dictionaries. The first question you’ll ask is, are items in a dictionary ordered?

02:05 This is something that has changed in the relatively recent past in Python.

02:10 Do dictionaries have methods to reorder items within them in the same way that lists, for example, have such methods? You’ll then move on to refresh your memory on how the built-in function sorted() works. Often, you would use sorted() with sequences such as lists.

02:28 Next, you’ll focus more on dictionaries, which is the main purpose of this course. You’ll explore what views in a dictionary are, and then you’ll find out how you can use the same built-in function sorted(), but this time to sort dictionaries, and you’ll also go a step further and sort dictionaries using more complex sorting rules that might be more suited to the application you’re looking at.

02:53 Right, so let’s get started.

Become a Member to join the conversation.