🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpython

python Documentation

LOADING ENGINE...

sorted()

AI & DATA SCIENCE // sorted

sorted() returns a new, sorted list built from the items of any iterable, leaving the original untouched.

Syntax

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

Deep Dive Course

Unlike list.sort(), which sorts a list in place and returns None, sorted() works on any iterable, not just lists, always returns a brand-new list, and never modifies its input. Its key parameter takes a function used to compute a comparison value for each element, for example sorting case-insensitively or by length, and reverse=True flips the order to descending. Python's sort algorithm, Timsort, is stable, meaning equal elements keep their original relative order.

1Understanding sorted()

Unlike list.sort(), which sorts a list in place and returns None, sorted() works on any iterable, not just lists, always returns a brand-new list, and never modifies its input. Its key parameter takes a function used to compute a comparison value for each element, for example sorting case-insensitively or by length, and reverse=True flips the order to descending. Python's sort algorithm, Timsort, is stable, meaning equal elements keep their original relative order.

💡

sorted() and list.sort() both accept key= and reverse= — use sorted() when you need to keep the original order too, and .sort() only when you specifically want to mutate in place and don't need the old order.

editor.html
numbers = [5, 2, 8, 1, 9]
print(sorted(numbers))
print(sorted(numbers, reverse=True))
localhost:3000

2Practical Example

Here is a real-world application of sorted() showing how it is used in production Python code.

editor.html
students = [{"name": "Boris", "grade": 82}, {"name": "Ana", "grade": 91}]
ranked = sorted(students, key=lambda s: s["grade"], reverse=True)
print([s["name"] for s in ranked])
localhost:3000

3Best Practices

Follow these guidelines when working with sorted():

1. Use key= instead of a custom comparator function — it's simpler, and Python has removed the older comparator-function style entirely

2. Sort a dictionary's items by value using key= on the items() view instead of manual loops

3. Rely on Timsort's stability when you need a secondary sort key to preserve a previous sort's order for ties

⚠️

Tip: sorted() and list.sort() both accept key= and reverse= — use sorted() when you need to keep the original order too, and .sort() only when you specifically want to mutate in place and don't need the old order.

editor.html
numbers = [5, 2, 8, 1, 9]
print(sorted(numbers))
print(sorted(numbers, reverse=True))
localhost:3000

Examples

Example 01Basic Usage
numbers = [5, 2, 8, 1, 9]
print(sorted(numbers))
print(sorted(numbers, reverse=True))
Example 02Advanced Example
students = [{"name": "Boris", "grade": 82}, {"name": "Ana", "grade": 91}]
ranked = sorted(students, key=lambda s: s["grade"], reverse=True)
print([s["name"] for s in ranked])

Best Practices

  • Use key= instead of a custom comparator function — it's simpler, and Python has removed the older comparator-function style entirely
  • Sort a dictionary's items by value using key= on the items() view instead of manual loops
  • Rely on Timsort's stability when you need a secondary sort key to preserve a previous sort's order for ties

Interview Question

What's the difference between calling sorted() on a list and calling the list's own sort() method?

Hint: Think about return values and whether the original is modified.

sorted() returns a brand-new sorted list and leaves the original unchanged, and it works on any iterable, not just lists. The list's own sort() method sorts the list in place, mutating it directly, works only on lists, and returns None — so a common bug is reassigning a variable to the result of calling sort(), which sets it to None instead of the sorted list.

Exercises

MediumPractice using sorted() in a real scenario.
View Solution
numbers = [5, 2, 8, 1, 9]
print(sorted(numbers))
print(sorted(numbers, reverse=True))

Frequently Asked Questions

What's the difference between calling sorted() on a list and calling the list's own sort() method?

sorted() returns a brand-new sorted list and leaves the original unchanged, and it works on any iterable, not just lists. The list's own sort() method sorts the list in place, mutating it directly, works only on lists, and returns None — so a common bug is reassigning a variable to the result of calling sort(), which sets it to None instead of the sorted list.

Related Functions

list.sort()key parameterreversed()