Loading…
A short voice session: one question, one follow-up, then instant feedback — up to 5 minutes.
sorted() and list.sort() use Timsort, which is stable and O(n log n); use key= to sort by computed values, for example key=lambda u: (u.last_name, -u.age), or rely on stability with two passes.
list.sort() sorts in place and returns None, while sorted() returns a new list from any iterable. The key function is called once per element and its results are compared, which is faster and clearer than old-style comparison functions. Tuples compare element by element, so a tuple key handles multi-level sorting; negating a number reverses its order. When a field cannot be negated, such as a string to sort descending, sort by the secondary key first and then by the primary key, because Timsort is stable and keeps equal elements in their previous order. operator.attrgetter and itemgetter are efficient alternatives to lambdas.