On this page
  1. sorted
  2. Import & Availability
  3. Overview & Signature
  4. Usage
  5. Gotchas
  6. REPL Inspect

sorted

Prev: slice | Up: __builtins__ | Next: staticmethod
Rel: all, any, enumerate, filter, iter
Ref: docs.python.org — sorted
Tags: #public #python3 #builtins


Import & Availability

# Built-in (available globally without import)
from builtins import sorted

Overview & Signature

sorted(iterable, /, *, key=None, reverse=False)

Return a new list containing all items from the iterable in ascending order.

Usage

words = ["pear", "fig", "apple"]
assert sorted(words, key=len) == ["fig", "pear", "apple"]
assert words == ["pear", "fig", "apple"]   # original list is untouched

Gotchas

REPL Inspect

help(sorted)

Linked from

Related notes