Summary: The slice notation
list[::-1] with default
stop indices and negative step size
-1 reverses a given
Problem: Given a list of elements. How to reverse the order of the elements in the list.
Example: Say, you’ve got the following list:
['Alice', 'Bob', 'Carl', 'Dora']
Your goal is to reverse the elements to obtain the following result:
['Dora', 'Carl', 'Bob', 'Alice']
Slicing with Default Start and Stop Values
Slicing is a concept to carve out a substring from a given string.
Use slicing notation
s[start:stop:step] to access every
step-th element starting from index
start (included) and ending in index
All three arguments are optional, so you can skip them to use the default values (
step=1). For example, the expression
s[2:4] from string
'hello' carves out the slice
'll' and the expression
s[:3:2] carves out the slice
'hl'. Note that slicing works the same for lists and strings.
You can use a negative step size (e.g., -1) to slice from the right to the left in inverse order. Here’s how you can use this to reverse a list in Python:
# Reverse a List with Slicing names = ['Alice', 'Bob', 'Carl', 'Dora'] names = names[::-1] print(names) # ['Dora', 'Carl', 'Bob', 'Alice']
Python masters know slicing from the inside out. Do you want to improve your slicing skills? Check out my book “Coffee Break Python Slicing” that will make you a slice pro in no time!
Alternatives Reversing List
Alternatively, you can also use other methods to reverse a list.
list.reverse()— Best if you want to reverse the elements of list in place.
list[::-1]— Best if you want to write concise code to return a new list with reversed elements.
reversed(list)— Best if you want to iterate over all elements of a list in reversed order without changing the original list.
list.reverse() can be 37% faster than
reversed(list) because no new object has to be created.
Try it yourself in our interactive Python shell:
Exercise: Run the code. Do all methods result in the same reversed list?
Where to Go From Here?
Enough theory. Let’s get some practice!
Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.
To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
You build high-value coding skills by working on practical coding projects!
Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?
🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.