Sort Your Lists
00:00 I want to show you how you can sort your lists, and you’ll see this is a lot more straightforward than copying lists.
00:07
If you have a list that contains the names of colors—here I have a list that contains the strings "red"
, "yellow"
, "green"
, and "blue"
as elements of the colors list—then you can sort it by calling the .sort()
method on the list
object.
00:22
To do this, you write colors.sort()
and then call the .sort()
method by using the parentheses. Now if you press Enter, there’s no output in the IDLE shell because the .sort()
method works on the list object directly.
00:39
So if you now inspect colors
again, you’ll see that it has a different order. That means that the elements inside the list got ordered, in this case alphabetically because you’re dealing with strings.
00:51
So "blue"
starts with a b
, so it’s now the first element in the list, and then you have "green"
, "red"
, and "yellow"
.
00:58
You can do the same with lists of numbers as well. So if you have a numbers list that contains the integers, let’s say 1
, 10
, 5
, and 3
, and then you can again say numbers.sort()
and inspect the numbers
list again.
01:13
Then now it’s ordered starting from the lowest integer, 1
, then going to 3
, 5
, and then 10
.
01:21
So just by calling the .sort()
method, you can sort your lists. And with strings, it’s a bit interesting because I mentioned that it’s alphabetically, and for quite a few use cases this is going to do what you expect it to do, but it’s actually quite a bit more complex behind the scenes because it sorts according to Unicode code points.
01:43 I’m not going to go into this much now, but I am actually curious what it would do with the color dots that we’ve worked with earlier. I have not tried this out, but let’s use this different colors list again with just the emoji symbols, and now I’m going to see how it will sort that list just out of curiosity.
02:04 Okay, so it looks like red gets sorted first, then blue, then yellow, then green.
02:13 The details of how this sorting actually works behind the scenes is fascinatingly complex. So if you’re curious about a big rabbit hole, then I can recommend this tutorial on how to sort Unicode strings alphabetically in Python.
02:27 But you don’t have to think about this at all if you don’t want to because most likely Python’s going to sort the way that you expect it to. Anyway, very sidetracking.
02:38
The takeaway is you can sort lists using the .sort()
method and most of the times, it’s going to work as you expected. If you work with normal characters, then it’ll sort them alphabetically.
02:48
If you work with numbers, it’s going to sort them ascending. You can also sort them descending or in reverse by passing the reverse=True
argument.
02:59
Let’s try that out with the original colors list. So I’ll copy this one and then say colors.sort(reverse=True)
.
03:12 And now if you inspect colors, you’ll see that it just goes the other way. So blue is now the last element in the list, and yellow is the first one,
03:22
and you can do the same thing also with a list of numbers. What reverse=True
does is essentially inverts the results of using .sort()
without setting reverse
to True
.
Become a Member to join the conversation.