Often, we will want to sort a list in either numerical (1, 2, 3, …) or alphabetical (a, b, c, …) order.
We can sort a list using the method
Suppose that we have a list of names:
names = ["Xander", "Buffy", "Angel", "Willow", "Giles"]
Let’s see what happens when we apply
['Angel', 'Buffy', 'Giles', 'Willow', 'Xander']
As we can see, the
.sort() method sorted our list of
names in alphabetical order.
.sort() also provides us the option to go in reverse. Instead of sorting in ascending order like we just saw, we can do so in descending order.
['Xander', 'Willow', 'Giles', 'Buffy', 'Angel']
.sort() method does not return any value and thus does not need to be assigned to a variable since it modifies the list directly. If we do assign the result of the method, it would assign the value of
None to the variable.
Let’s experiment sorting various lists!
.sort() to sort
print() to see how
# and whitespace in front of the line
sort(names). Edit this line so that it runs without producing a
sorted_cities. Why is it not the sorted version of
.sort() call on
cities such that it sorts the cities in reverse order (descending).
cities to see the result.