10-02-2020, 01:24 AM
Python Reverse List with Slicing — An Illustrated Guide
Summary: The slice notation with default and indices and negative step size reverses a given .
![[Image: blogMostPythonicWay-1024x576.jpg]](https://blog.finxter.com/wp-content/uploads/2020/10/blogMostPythonicWay-1024x576.jpg)
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:
Your goal is to reverse the elements to obtain the following result:
Slicing with Default Start and Stop Values
Slicingis a concept to carve out a substring from a given string.
Use slicing notation to access every -th element starting from index (included) and ending in index (excluded).
All three arguments are optional, so you can skip them to use the default values (, , ). For example, the expression from string carves out the slice and the expression carves out the slice . 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:
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.
The method can be 37% faster than because no new object has to be created.
Try it yourself in our interactive Python shell:
https://trinket.io/embed/python/c6fcdd41c9
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!
To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And 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?
Practice projects is how you sharpen your saw in coding!
Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?
Then become 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.
Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.
Join the free webinar now!
The post Python Reverse List with Slicing — An Illustrated Guide first appeared on Finxter.
Summary: The slice notation
Code:
list[::-1]Code:
startCode:
stopCode:
-1Code:
list![[Image: blogMostPythonicWay-1024x576.jpg]](https://blog.finxter.com/wp-content/uploads/2020/10/blogMostPythonicWay-1024x576.jpg)
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:
Code:
['Alice', 'Bob', 'Carl', 'Dora']Your goal is to reverse the elements to obtain the following result:
Code:
['Dora', 'Carl', 'Bob', 'Alice']Slicingis a concept to carve out a substring from a given string.
Use slicing notation
Code:
s[start:stop:step]Code:
stepCode:
startCode:
stopAll three arguments are optional, so you can skip them to use the default values (
Code:
start=0Code:
stop=len(lst)Code:
step=1Code:
s[2:4]Code:
'hello'Code:
'll'Code:
s[:3:2]Code:
'hl'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:
Code:
# 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.
- — Best if you want to reverse the elements of list in place.Code:
list.reverse()
- — Best if you want to write concise code to return a new list with reversed elements.Code:
list[::-1]
- — Best if you want to iterate over all elements of a list in reversed order without changing the original list.Code:
reversed(list)
The method
Code:
list.reverse()Code:
reversed(list)Try it yourself in our interactive Python shell:
https://trinket.io/embed/python/c6fcdd41c9
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!
To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And 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?
Practice projects is how you sharpen your saw in coding!
Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?
Then become 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.
Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.
Join the free webinar now!
The post Python Reverse List with Slicing — An Illustrated Guide first appeared on Finxter.

