The most efficient method to join a list of Python strings in reverse order is to use the Python code
''.join(l[::-1]). First, reverse the list
l using slicing with a negative step size
l[::-1]. Second, glue together the strings in the list using the
join(...) 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
l = ['n', 'o', 'h', 't', 'y', 'p']
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.
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
list[start:stop:step] to create a sublist starting with index
start, ending right before index
stop, and using the given
step 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:
l = ['n', 'o', 'h', 't', 'y', 'p'] # Method 1 print(''.join(l[::-1])) # python
string.join(iterable) method joins the string elements in the
iterable to a new string by using the
string on which it is called as a delimiter.
You can call this method on each list object in Python. Here’s the syntax:
|The elements to be concatenated.|
- 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
reversed() 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.
l = ['n', 'o', 'h', 't', 'y', 'p'] print(''.join(reversed(l))) # python
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 loop where it’s not really needed. Anyways, especially coders coming from other programming languages like Java or C++ will often use this approach.
l = ['n', 'o', 'h', 't', 'y', 'p'] s = '' for x in reversed(l): s += x print(s) # python
However, 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
sand introduces a higher level of complexity.
You can see this in our interactive memory visualizer:
Exercise: click “Next” to see the memory objects used in this code snippet!
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.