Meaning
Returns a shallow copy of a list with elements in reverse order. Used when you need a reversed version without modifying the original.
Primary Function
List manipulation
Communicative Purpose
Provides a quick, readable way to obtain a reversed sequence.
Pattern
seq[::-1]
Core Structure
...[::-1]
Função primária
List manipulation
Propósito comunicativo
Provides a quick, readable way to obtain a reversed sequence.
Situações de gatilho
Python scripting: need to iterate over a list backwards; Data processing: reverse order of records for display; Algorithm design: check palindrome by reversing input sequence
Contextos
Found in Python scripts, data processing pipelines, algorithmic challenges, and any codebase that works with lists.
Padrão
seq[::-1]
Estrutura central
...[::-1]
Slots de substituição
list_expression: any expression that evaluates to a list (or sequence)
Colocados típicos
- for loops
- len()
- reversed()
- list.copy()
Substituições comuns
- reversed(seq) returns an iterator
- list.reverse() modifies in-place
- seq[::-1] creates a new list
Erros comuns
Assuming the slice modifies the original list; using it on mutable objects expecting in-place change; forgetting it creates a shallow copy.
Similar / contraste
reversed() built-in (lazy iterator); list.reverse() method (in-place reversal).
Interferências
Coming from languages like C or Java where manual index loops are typical; expecting in-place reversal like list.reverse().
Família do chunk
- list slicing
- reversed()
- list.reverse()
Nuance
Works on any sequence (list, tuple, string) returning the same type; O(n) time and space; creates a shallow copy; for large data prefer reversed() for lazy iteration.
Efeito pragmático
Makes intent explicit and concise, improving readability.
Dica de memória
Double colon, negative one flips it.
Nota
Produces a shallow copy; nested mutable objects are shared with the original.
Upgrade path
Using reversed() for lazy iteration: for item in reversed(my_list): ...
Log in to save chunks.