How to Reverse a String or List in Python (4 Methods)
Discover idiomatic Python techniques to reverse sequences, complete with time and space complexity trade-offs.
Concepts & Theory
The step slicing syntax `sequence[::-1]` is the fastest and most concise idiom to reverse a string or list in Python, generating a new inverted copy in O(n) time.
The built-in `reversed()` function returns a lazy reverse iterator. It consumes O(1) auxiliary memory and is optimal when streaming values into a for loop.
For mutable lists, `list.reverse()` performs the inversion in place without allocating extra memory (O(1) space).
Code Example
Run in Playground# Méthode 1 : Slicing élégant (chaînes & listes)
texte = "Carthage"
inverse = texte[::-1]
print("Slicing :", inverse) # 'egahtraC'
# Méthode 2 : reversed() avec join
mots = ["Python", "IA", "Code"]
print("Itérateur :", list(reversed(mots))) # ['Code', 'IA', 'Python']
# Méthode 3 : En place sur liste (zéro copie mémoire)
nombres = [1, 2, 3, 4, 5]
nombres.reverse()
print("En place :", nombres) # [5, 4, 3, 2, 1]Common Pitfalls & Mistakes
✗ Calling .reverse() on an immutable string
text = "hello"
text.reverse() # AttributeErrortext = "hello"
reversed_text = text[::-1]Strings are immutable in Python. The `.reverse()` method only exists on mutable list objects.
Frequently Asked Questions
Which method is fastest for reversing strings in Python?▾
Step slicing `text[::-1]` is executed in optimized C within CPython, making it the fastest method in benchmark runtime.
Master Python with interactive exercises
Complete hands-on code challenges in your browser with real-time feedback and progressive hints.
