Sorting Dictionaries in Python: Keys, Values, and More (Summary)
You’ve gone from the most basic way to sort a dictionary to a few advanced alternatives that consider performance in sorting key-value pairs.
In this video course, you’ve:
- Reviewed the
sorted()
function - Discovered dictionary views
- Understood how dictionaries are cast to lists during sorting
- Specified sort keys to sort a dictionary by value, key, or nested attribute
- Used dictionary comprehensions and the
dict()
constructor to rebuild your dictionaries - Considered whether a sorted dictionary is the right data structure for your key-value data
You’re now ready to not only sort dictionaries by any criteria you might think of, but also to judge whether the sorted dictionary is the best choice for you.
Share your sorted dictionary use cases and performance comparisons in the comments below!
Congratulations, you made it to the end of the course! What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment in the discussion section and let us know.
00:00 You’ve made it to the end of this course. Well done. Let’s summarize the main learning outcome from the scores on sorting a dictionary in one slide.
00:10 This dictionary is one you’ve seen before. It’s the points that Jill, Jack, Jim, and Jane have in a game that they’re playing. Now, the main characteristic of a dictionary is the link, the association between the key and the value, in this case, between the string containing the name of the players and the integer, showing how many points each player has.
00:34
Before Python 3.6, dictionaries had no order. There was no guarantee in which order the items will come up if you are displaying them or if you are looping through them using a for
loop, for example.
00:46 However, in Python 3.6, this changed and more formally in Python 3.7, there is now a guarantee that the order in which you add items in a dictionary will be maintained and therefore you can rely on this order if you want to.
01:02 And one reason you may want to use the order of items in a dictionary is if you want to sort it based on some condition. In this case, you want to sort it based on the number of points. And the process is often as follows.
01:16 You will decide what view of the dictionary you want to use. In this case, you’re using the items view. The items view groups the key and the value together as a tuple.
01:26
So the first argument in the built-in function sorted()
in this case is points.items()
.
01:32
The second argument is the argument that is assigned to the key
parameter. This is the rule you want sorted()
to use. There are a number of options you can use in here, as you’ve seen in the scores, in this slide, you can see the lambda
function option where you have a lambda
function that accepts item
, this will be the tuple containing key, value
.
01:53
And the lambda
function returns the value item
[1]
, is the second item in the tuple so the value. This means that sorted()
will use the value of each item as a way of deciding where to place this item.
02:07
And the final argument in sorted()
is reverse=True
because you would like the highest value first.
02:14
That’s not all because sorted()
, the built-in function sorted()
always returns a list. In this case, since points.items()
is the items view, the list will be a list of tuples.
02:25
If you want a dictionary, you can cast that list into a dictionary using the dict()
constructor, and that will give you a new dictionary ordered in the way you want it based on the number of points.
02:40 So let’s finish the scores by looking at the main things you learned. You started by reviewing the ordering of elements in the Python dictionary. This is something that has changed in Python 3.6 and more formally in Python 3.7.
02:53 And the order of items in the dictionary is now maintained. You’ve also explored the views in the dictionary. You’ve got the keys, the values, and the items view.
03:02
And the items view where the keys and the values are paired together in the tuple is the one you will often use when you want to sort the dictionary. And then you’ve looked at the sorted()
built-in function and how you can use it to sort dictionaries, especially using the key
parameter, which allows you to set any rule you want on how you want your dictionaries sorted.
03:24
And if you want to find out more about these topics, there is an article and a matching video course about sorted()
and .sort()
in Python that talks more about sorting in general sequences and dictionaries as well.
03:38 And there’s an in-depth tutorial about dictionaries in Python.
03:43 Hope you’ve enjoyed this course and next time you’re dealing with dictionaries and you might think, I would like to sort this dictionary a different way you now know how to do that.
Stephen Gruppetta RP Team on Aug. 15, 2024
Thanks Mike! Glad you found it useful. And you’re right, pausing these videos and trying things out ahead of the instructor is a useful tip. I’ll make sure to mention it more in my next Real Python video courses
Become a Member to join the conversation.
mikehillsnc on Aug. 13, 2024
Very useful. I have been using .sort() lists and learned the features of sorted()
itemgetter was new to me but I don’t have any immediate application for it
One minor thought - when u mention lambda you might suggest reader stops and tries it for themselves before revealing answer