Skip to content
Built-in Functions

sorted()

Make a new list containing items in sorted order.

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

sorted() returns a new sorted list from the items in any iterable without modifying the original collection.

Parameters

NameDescription
iterableThe items to sort.
keyOptional function used to choose a sorting value.
reverseUse True to sort from largest to smallest.

Returns

A new sorted list.

Try it

Run it and change it
scores = [12, 5, 9]
print(sorted(scores))

Worth knowing

  • sorted() vs list.sort(): sorted() works on any iterable (strings, tuples, sets, dicts) and returns a new list; list.sort() only works on lists in place.

Related entries