06-11-2020, 04:34 PM
The Most Pythonic Way to Join a List in Reverse Order
The most efficient method to join a list of Python strings in reverse order is to use the Python code. First, reverse the list using slicing with a negative step size . Second, glue together the strings in the list using the method on the empty string .
Problem: Given a list of strings. How to join the strings in reverse order?
Example: You want to join the following list
to obtain the joined string in reverse order
Let’s get a short overview on how to accomplish this.
Solution Overview: You can try the three methods in our interactive Python shell.
https://trinket.io/embed/python/8a6ba00cf3
Next, we’ll dive into each method separately.
Method 1: Join + Slicing
The first and most Pythonic method to reverse and join a list of strings is to use slicing with a negative step size.
You can slice any list in Python using the notation to create a sublist starting with index , ending right before index , and using the given size—which can also be negative to slice from right to left. If you need a refresher on slicing, check out our detailed Finxter blog tutorial or our focused book “Coffee Break Python Slicing”.
Here’s the first method to reverse a list of strings and join the elements together:
The method joins the string elements in the to a new string by using the on which it is called as a delimiter.
You can call this method on each list object in Python. Here’s the syntax:
Argument |
Description
|
The elements to be concatenated.
Related articles:
Method 2: Join + reversed()
The second method is also quite Pythonic: using the built-in function rather than slicing with a negative step size. I’d say that beginners generally prefer this method because it’s more readable to them—while expert coders prefer slicing because it’s more concise and slightly more efficient.
The join method glues together all strings in the list—in reversed order!
Method 3: Simple Loop
The third method is the least Pythonic one: using a loopwhere it’s not really needed. Anyways, especially coders coming from other programming languages like Java or C++ will often use this approach.
However, there are several disadvantages. Can you see them?
You can see this in our interactive memory visualizer:
https://pythontutor.com/iframe-embed.htm...nces=false
Exercise: click “Next” to see the memory objects used in this code snippet!
Related articles:
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 most efficient method to join a list of Python strings in reverse order is to use the Python code
Code:
''.join(l[::-1])Code:
lCode:
l[::-1]Code:
join(...)Code:
''Problem: Given a list of strings. How to join the strings in reverse order?
Example: You want to join the following list
Code:
l = ['n', 'o', 'h', 't', 'y', 'p']to obtain the joined string in reverse order
Code:
pythonLet’s get a short overview on how to accomplish this.
Solution Overview: You can try the three methods in our interactive Python shell.
https://trinket.io/embed/python/8a6ba00cf3
Next, we’ll dive into each method separately.
Method 1: Join + Slicing
The first and most Pythonic method to reverse and join a list of strings is to use slicing with a negative step size.
You can slice any list in Python using the notation
Code:
list[start:stop:step]Code:
startCode:
stopCode:
stepHere’s the first method to reverse a list of strings and join the elements together:
Code:
l = ['n', 'o', 'h', 't', 'y', 'p'] # Method 1
print(''.join(l[::-1]))
# pythonThe
Code:
string.join(iterable)Code:
iterableCode:
stringYou can call this method on each list object in Python. Here’s the syntax:
Code:
string.join(iterable)Argument |
Description
Code:
iterableThe elements to be concatenated.
Related articles:
- Python Join List [Ultimate Guide]
- Python Slicing [Ultimate Guide]
- How to Reverse a Python List
- The Ultimate Guide to Python Lists
Method 2: Join + reversed()
The second method is also quite Pythonic: using the
Code:
reversed()Code:
l = ['n', 'o', 'h', 't', 'y', 'p']
print(''.join(reversed(l)))
# pythonThe join method glues together all strings in the list—in reversed order!
Method 3: Simple Loop
The third method is the least Pythonic one: using a loopwhere it’s not really needed. Anyways, especially coders coming from other programming languages like Java or C++ will often use this approach.
Code:
l = ['n', 'o', 'h', 't', 'y', 'p'] s = ''
for x in reversed(l): s += x
print(s)
# pythonHowever, there are several disadvantages. Can you see them?
- The code is less concise.
- The code is less efficient because of the repeated string concatenation. Each loop execution causes the creation of a new string, which is highly inefficient.
- The code requires the definition of two new variables andCode:
xand introduces a higher level of complexity.Code:s
You can see this in our interactive memory visualizer:
https://pythontutor.com/iframe-embed.htm...nces=false
Exercise: click “Next” to see the memory objects used in this code snippet!
Related articles:
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!

