Skip to Content
Working with Lists in Python
Sorting Lists I

Sometimes, we want to sort a list in either numerical (1, 2, 3, …) or alphabetical (a, b, c, …) order.

We can sort a list in place using .sort(). Suppose that we have a list of names:

names = ['Xander', 'Buffy', 'Angel', 'Willow', 'Giles'] print(names)

This would print:

['Xander', 'Buffy', 'Angel', 'Willow', 'Giles']

Now we apply .sort():

names.sort() print(names)

And we get:

['Angel', 'Buffy', 'Giles', 'Willow', 'Xander']

Notice that sort goes after our list, names. If we try sort(names), we will get a NameError.

sort does not return anything. So, if we try to assign names.sort() to a variable, our new variable would be None:

sorted_names = names.sort() print(sorted_names)

This prints:


Although sorted_names is None, the line sorted_names = names.sort() still edited names:

>>> print(names) ['Angel', 'Buffy', 'Giles', 'Willow', 'Xander']



Use sort to sort addresses.


Use print to see how addresses changed.


Remove the # in front of the line sort(names). Edit this line so that it runs without producing a NameError.


Use print to examine sorted_cities. Why is it not the sorted version of cities?

Folder Icon

Sign up to start coding

Already have an account?