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

Summary

In this lesson, you learned that:

  • sorted(values) returns a new list with the values from the smallest to the largest, and leaves the original as it was. It takes anything a for loop can go through (a list, a tuple, a set, a dictionary's keys) and always returns a list. That's how you print a set in the same order every time: print(sorted(guests)).
  • Texts are compared letter by letter, by the codes of their letters: uppercase letters come before lowercase ones ("Zoe" before "adam"), and letters with accents after z. Numbers written as text are sorted as text, so convert them with int() first.
  • values.sort() sorts the list itself and returns None. Never store what it returns: after names = names.sort(), the list is gone. Only lists have .sort().
  • reverse=True sorts from the largest. sorted(scores, reverse=True)[:3] are the three highest scores.
  • key takes a function, written without parentheses. Python calls it on each element and sorts the elements by what it returns: key=len sorts by length, key=str.lower without caring about uppercase and lowercase. Elements with the same key keep their order.
  • The key function can be your own: by_score(player), which returns player[1], sorts a list of tuples by their second elements, and sorted(scores.items(), key=by_score) sorts the pairs of a dictionary by their values.
  • max() and min() take key too, and return the element with the largest or the smallest key: max(words, key=len) is the longest word.

Now practice it on the results of a long jump competition. In the next lesson, List Comprehensions, you'll learn to build a new list from another one in a single line, for example the lengths of all the words in a list.

Instructions

  1. 1

    Print the names of the jumpers in alphabetical order, joined into one line with ", ".join(): Anna, Eva, Lily, Max, Tom. Leave the line with the results as it is: the exercise also runs your program with other results.

  2. 2

    Below, print the three best jumpers from the longest jump, each on its own line with their result, for example Eva: 435. Write your own key function for it.

Back

9/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.