- [Narrator] In this video, I'd like to
take a a look at sorting the elements of a list
and then showing you another way to do sorting,
which will work not only with lists,
but also with the contents of a string
or with tuples as well.
So for the purpose of this example to start out,
let's create this list called numbers.
And as you can see, I've purposely created the list
out of order, so that we can demonstrate
sorting capabilities.
Now, it turns out that built in to the list type
is a sort method that allows you to modify
a list in place.
So if I type numbers, and a dot, and tab,
you can see all the different methods available to you
for the list type in Python,
one of which is sort.
So lets go ahead and invoke sort.
And that's an in-place sort, so now if I look at numbers
after the operation,
you can see that indeed
it modified the list and now the list is in
ascending order by default.
Now let me redefine that list once again,
because you can also sort in descending order by default.
Not by default, but by passing an argument
to the sort method.
So if we say reverse=true, that says
I would like to sort in descending order.
So now if I take a look at the values in numbers,
I can see them top to bottom.
Ten down to one.
So that's in-place sorting with the sort method
of a list.
Now, if you don't want to modify the list's contents
or if you're working with a sequence that's immutable,
there's also a built-in function for Python
called sorted.
So let's go ahead once again and create that list
with the elements out of order.
And for this one, we'll assign the values
to a variable after the operation that's going
to sort those elements.
So let's say we have a variable called
ascending_numbers and we're going to assign that
sorted, with numbers as an argument.
Now, sorted is the built-in function.
It takes as its argument an iterable object,
some sort of sequence I can iterate over.
And it gives back a list of values
in ascending order by default.
So if I now go and display the contents
of ascending_numbers,
we can see that indeed, this new variable
contains the values in ascending order,
but it did not modify numbers.
And to prove that, we can evaluate numbers
and see that indeed, it still contains
the original values that we defined
back in snippet seven.
So that's sorted with a list,
but I also mentioned the fact that you can use sorted
with immutable types as well.
So let's go and create a string called letters.
We just have a bunch of random letters here
and they are clearly not in alphabetical order.
So let's define that.
And we can go ahead and use the sorted method,
or function rather, with letters to get the
characters in this case in alphabetical order.
So let's call this ascending_letters.
And I'm going to assign the result to a variable
for a moment, just to prove to you that the original
letters string is left unmodified.
Now, of course a string is imutable in the first place,
so that should still be the case.
So let's go ahead and sort the letters string.
And now if I evaluate ascending_letters,
you can see that it gives me back a list,
not a string.
A list of the individual letters in alphabetical order.
And actually it's not truly alphabetical order,
it's in lexicographical order,
where it's ordering those characters based on
their underlying numeric values in the character set
used underneath the hood,
which is the Unicode character set.
So here you see that we can in fact sort
the contents of the string letters,
but we get that back as a list
containing the individual letters.
And just to show you that letters remains unchanged,
there is the original letters string.
Now, you can also use this with tuples.
So let's say I have the following tuple,
which is just the collection of strings red, orange,
yellow, green, blue.
And let's create a new variable once again.
So we'll call this one ascending_colors
and let's sort that.
So hopefully now when we evaluate ascending_colors,
we will see those, in this case what appears
to be alphabetic order,
but again, it's really lexicographical order.
So if I had uppercase and lowercase letters
in some of these strings,
they may not appear in the same order
that you're seeing right now.
And separately, notice that it gives me back a list,
even though I started with a tuple.
And finally, notice that if I display colors,
it is still a tuple with the strings in their
original order.
So as you saw here,
we were able to sort lists in-place with the sort method
and we were able to sort lists, string contents, or tuples,
with the sorted built-in function.
The one thing I didn't demonstrate here
is the fact that you can also sort in descending order
with the sorted method.
So for example, if I say sorted,
and let's say we want to sort these color strings
in descending order.
We can give a second argument that says
reverse=True, just like we did with the sort method
built in to a list,
and we get back a list containing those strings now
in descending order from highest to lowest.
And again, because I'm using all lowercase letters,
they're in alphabetical order in descending order
in this case.