A second way of sorting a list in Python is to use the built-in function sorted().

The sorted() function is different from the .sort() method in two ways:

  1. It comes before a list, instead of after as all built-in functions do.
  2. It generates a new list rather than modifying the one that already exists.

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:


Would output:

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



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


Print both games and games_sorted. How are they different?

Sign up to start coding

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?