Sorting Dictionaries in Python
00:00
You’re now familiar with the built-in functions sorted()
, which accepts an iterable and returns the list with items in order. But dictionaries are also iterable.
00:10
You can consider the dictionary points
that you used earlier. This dictionary has four items. The keys are the names, Jill, Jack, Jim, and Jane, and the values are the number of points that each player has in a game that they’re playing.
00:25
So you can now call sorted()
with points
, the dictionary, which is also iterable and sorted()
returns the list, but only the names of the players are included in this list.
00:37
So only the keys of the dictionary. When sorted()
acts on a dictionary, it only considers the keys, ignoring the values.
00:48
This is a similar result to when you try to iterate over a dictionary, for example, for item
in points
, let’s say you just want to print(item)
to see what it looks like and the for
loop works, but it only iterates over the keys, not the values.
01:06
When dealing with a dictionary, you can access either just the keys using the .keys()
method. This gives you a view of the keys. It doesn’t create a copy of them.
01:16 It’s simply giving you a read-only view of what the keys in the dictionary are.
01:23
You can also get a view of the values using the .values()
method or, and this is often quite useful as we’ll see if you want to keep both the keys and the values, you can get a view of the items using the .items()
method, and in this case, you can see that the view contains a structure that has Jill and 10 grouped together in a tuple, and then Jack and eight are grouped together, Jim and 12 and Jane and 5.
01:54
So what would happen if you call sorted()
instead of on points
directly on the view returned by points.items()
?
02:03 So let’s have a look. We got a list of tuples and the first one is Jack eight. So Jack with 8 points followed by Jane 5, Jill 10, and Jim 12.
02:15 The way Python sorts tuples is to look at the first item in each tuple and sort those first. If there are two that are equal, then it’ll look at the second item in the tuple.
02:29 And if the tuples had more items, Python will keep checking each successive element. But in this case, because Jack, Jane, Jill, and Jim are all distinct keys, they are sorted in alphabetical order with Jack first, then Jane, followed by Jill and Jim.
02:47
But you can also use the key
parameter in the sorted()
function. First, you can define a helper function, let’s call it value_getter()
, which accepts an item
03:01
and this function simply returns the item’s second element, so the item will be the tuple, for example, the tuple (‘Jack’, 8) or (‘Jane’, 5), an item that comes from points.items()
and the square brackets one gives us the second element of the tuple, which is the value from the dictionary.
03:25
You can now use this function value_getter()
with the key
parameter. So you can call sorted()
with points.items()
as you did earlier, but now you can also pass the key
parameter with value_getter()
, the name of the function you just defined.
03:44 In this case, you can see the order is different. Jane is first, Jane and her 5 points, followed by Jack, Jill, and Jim.
03:52 The items have now been sorted based on their second element, which is the value in the dictionary. So you’ve been able to sort your dictionary using the values rather than the keys. In this case, lowest points first, highest points last.
04:11
However, this is not a dictionary. sorted()
, in this case, when you pass points.items()
as an argument, it returns a list of tuples.
04:20
If you want to end the process with another dictionary sorted()
based on the values, you can cast whatever is returned from sorted()
into a dictionary.
04:32
So the list of tuples returned by sorted()
now becomes a dictionary. The items are the same, but they’re ordered based on the values. Jane is first with 5 points, then Jack, then Jill, and then Jim.
04:46 You have taken an unordered dictionary and sorted it based on the values of the dictionary.
04:55
And you can also, when you call sorted()
, add the reverse
parameter and set it to True
or rather pass True
to it.
05:04
And in this case, the sorted()
built-in function works in reverse. Therefore, Jim is first with 12 points, followed by Jill, Jack, and finally Jane, who’s got the fewest number of points.
05:17
And if you’re comfortable with lambda functions, you don’t even need to define the function value_getter()
. Instead, you can pass as the key
argument a lambda function that takes item
and returns the second element from item
.
05:36 And this works in exactly the same way. It returns the same dictionary sorted based on the values in reverse order.
05:46
Let’s look at the key points from this lesson before moving on to the next one. You can access views for the keys, values, or items in a dictionary. These are through the methods .keys()
, .values()
, .items()
.
06:00
And then you can use the built-in sorted()
function to sort these dictionaries. By default, a dictionary is sorted using its keys, however, you can use any of the dictionary views.
06:10
But normally you would use the items view, which is where the key and values are paired together in a tuple. You can also use the key
parameter to set a user-defined rule to sort your dictionary.
06:23 You’ve seen this in this lesson where you had a dictionary and you sorted it using the values. So the second item in the tuple that comes back from the items view.
06:34
And finally, you cast the list that’s returned by sorted()
back into a dictionary. So you start the process with a dictionary, sorted()
always returns a list, but if you want a dictionary at the end of the process, you can cast that list back into a dictionary.
Become a Member to join the conversation.