ProgrammingPython Developer

How does the sorted() function work in Python, how does it differ from the sort() method for lists, what nuances are there when using the key parameter and the reverse parameter?

Pass interviews with Hintsage AI assistant

Answer.

Background:

From the very beginning, the Python language has provided built-in tools for sorting collections. The sort() method and the sorted() function appeared in the second version of Python, but starting from Python 2.4, sorted() was introduced as a universal tool for sorting any iterable objects (not just lists).

Problem:

Often, there is a need to sort data by different criteria, and it is not always desirable to modify the original object. Beginners often confuse sort() and sorted(), not understanding where each is applied, and make mistakes with the sorting key and reverse order.

Solution:

  • sorted(iterable, key=None, reverse=False) always returns a new list object without modifying the original.
  • list.sort(key=None, reverse=False) sorts the list in place and returns None.
  • The key parameter is a function that determines the criterion for sorting.
  • reverse=True changes the order to reverse.

Example of sorting a list of strings by their length, in reverse order:

words = ['python', 'is', 'strong', 'language'] sorted_words = sorted(words, key=len, reverse=True) print(sorted_words) # ['language', 'python', 'strong', 'is']

Key features:

  • sorted() always returns a new list without touching the source
  • sort() only works with the list itself, does not return a new object
  • the key option can be any function that returns values for comparison

Tricky questions.

What does the sort() method return?

The list.sort() method sorts the list itself and always returns None, which is often forgotten.

lst = [3, 1, 2] res = lst.sort() print(res) # None print(lst) # [1, 2, 3]

Can a tuple be sorted using sort()?

No, tuples are immutable; they do not have a .sort() method. Use sorted() for sorting.

tuple_data = (5,2,3) sorted_tuple = sorted(tuple_data) print(sorted_tuple) # [2, 3, 5]

What happens if a function that returns different types is specified in key?

A TypeError will occur if the returned values are incomparable (for example, int and str).

Common mistakes and anti-patterns

  • Using sort() expecting a new sorted list
  • Trying to sort an immutable object with the sort() method
  • Providing a key function that returns values with heterogeneous types

Real-life example

Negative case

During an interview, the candidate is asked to return a sorted list by length, and they do:

lst = ['one', 'elephant'] ans = lst.sort(key=len) print(ans) # None

Pros:

  • Modifies the list in place (sometimes saves memory)

Cons:

  • Returns None, which breaks further logic

Positive case

Correct usage:

lst = ['one', 'elephant'] ans = sorted(lst, key=len) print(ans) # ['one', 'elephant']

Pros:

  • The original list is not modified
  • We get the expected result

Cons:

  • Takes additional memory for the new list