Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

Sorting with sorted()

A list keeps its elements in the order in which you put them there. Often you need them in another order: names from A to Z, scores from the lowest, prices from the cheapest. Python's built-in function sorted() does that for you:

scores = [72, 45, 90, 38, 64]
print(sorted(scores))
print(scores)
[38, 45, 64, 72, 90]
[72, 45, 90, 38, 64]

sorted() returns a new list with the same elements, sorted from the smallest to the largest. The original list stays as it was, as the second line shows. If you need the sorted list more than once, store it in a variable: ordered = sorted(scores).

Texts are sorted alphabetically. Python compares them letter by letter, like the words in a dictionary: first by their first letters, and when those are the same, by the second ones, and so on:

names = ["Tom", "Anna", "Max", "Eva"]
print(sorted(names))
['Anna', 'Eva', 'Max', 'Tom']

But Python doesn't know the alphabet the way you do. Inside the computer, every letter has a number, its code, and Python compares those. The uppercase letters A to Z have smaller codes than the lowercase letters a to z, and letters with accents, such as é, have larger codes than z, even as capitals:

print(sorted(["adam", "Zoe", "Bob", "Émile"]))
['Bob', 'Zoe', 'adam', 'Émile']

"Zoe" comes before "adam", because a capital Z comes before a small a. Later in this lesson, you'll sort texts without caring about uppercase and lowercase. Numbers written as text are compared letter by letter too: sorted(["10", "9", "25"]) gives ['10', '25', '9']. So numbers that come from .split() need int() before you sort them.

sorted() doesn't only take lists. It takes anything a for loop can go through, such as a tuple, a set or a dictionary (whose keys it sorts), and it always returns a list:

point = (3, 1, 2)
colors = {"red", "green", "blue"}
ages = {"Tom": 12, "Anna": 14, "Eva": 11}

print(sorted(point))
print(sorted(colors))
print(sorted(ages))
[1, 2, 3]
['blue', 'green', 'red']
['Anna', 'Eva', 'Tom']

That's handy for sets. A set has no order, so when you print one, its elements can come out in a different order every run. sorted() gives them to you in the same order every time.

Instructions

  1. 1

    Print temperatures sorted from the lowest, and on the next line temperatures itself, to see that the list hasn't changed. The output should be [12, 15, 18, 21, 25] and [18, 25, 12, 21, 15]. Leave the line with the temperatures as it is: the exercise also runs your program with other temperatures.

  2. 2

    guests is a set, so its order can change from run to run. Below, print the guests in alphabetical order, as a list: ['Anna', 'Eva', 'Max', 'Tom']. The exercise tries this with other guests too.

Back

1/9

  1. 1. Sorting with sorted()
  2. 2. The .sort() Method
  3. 3. Quiz: sort() or sorted()?
  4. 4. From the Largest
  5. 5. Sorting by Another Rule
  6. 6. Your Own Key
  7. 7. Quiz: Sorting
  8. 8. min() and max() with key
  9. 9. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.