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!
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.
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.
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. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, 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.