Skip to Content
Working with Lists in Python
Sorting Lists II

A second way of sorting a list is to use sorted. sorted is different from .sort() in several ways:

  1. It comes before a list, instead of after.
  2. It generates a new list.

Let’s return to our list of names:

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

Using sorted, we can create a new list, called sorted_names:

sorted_names = sorted(names) print(sorted_names)

This yields the list sorted alphabetically:

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

Note that using sorted did not change names:

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



Use sorted to order games and create a new list called games_sorted.


Use print to inspect games and games_sorted. How are they different?

Folder Icon

Sign up to start coding

Already have an account?