Meaning
Sorts a list in place in descending order. Use when you need the elements ordered from highest to lowest.
Primary Function
Sorting
Communicative Purpose
Arrange items in descending order efficiently.
Pattern
items.sort(reverse=True)
Core Structure
... .sort(reverse=True)
Função primária
Sorting
Propósito comunicativo
Arrange items in descending order efficiently.
Situações de gatilho
Data processing: need list sorted descending for ranking display; Algorithm implementation: require descending order for heap processing; Reporting: generate leaderboard where highest scores appear first
Contextos
Data processing scripts, algorithms, interview coding challenges, any Python code using list.sort.
Padrão
items.sort(reverse=True)
Estrutura central
... .sort(reverse=True)
Slots de substituição
items: list of comparable elements
Colocados típicos
- sorted()
- list.sort(key=...)
- reverse=False
Substituições comuns
- sorted(items
- reverse=True) returns new list
- items.sort() for ascending
Erros comuns
Assuming sort returns a new list; forgetting that sort is in-place; using reverse=1 instead of True
Similar / contraste
items.sort() for ascending order; sorted(items) returns new sorted list ascending
Interferências
Coming from languages where sort returns a new array (e.g., JavaScript), may expect a return value.
Família do chunk
- list.sort
- sorted
- reverse parameter
Nuance
Sort is stable and modifies the original list; if you need to keep original, use sorted(). Works only on lists; other sequences like tuples lack sort method.
Efeito pragmático
Efficient in-place sorting avoids extra memory allocation.
Dica de memória
Think 'reverse sort' as 'sort backwards'.
Nota
Remember that list.sort() sorts the list in place and returns None; to keep the original list, use sorted(items, reverse=True) instead.
Upgrade path
Using key function for custom ordering: items.sort(key=lambda x: x.attribute, reverse=True)
Log in to save chunks.