Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] The Most Pythonic Way to Join a List in Reverse Order

#1
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 ''.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

python

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

The 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:

string.join(iterable)


Argument Description
iterable The elements to be concatenated.

Related articles:

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 x and s and 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!

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!



https://www.sickgaming.net/blog/2020/06/...rse-order/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] The Most Pythonic Way to Get N Largest and Smallest List Elements xSicKxBot 0 2,050 09-01-2023, 03:23 AM
Last Post: xSicKxBot
  [Tut] Easiest Way to Convert List of Hex Strings to List of Integers xSicKxBot 0 1,458 11-25-2022, 11:54 AM
Last Post: xSicKxBot
  [Tut] Python Find Shortest List in List xSicKxBot 0 1,372 09-25-2022, 03:42 AM
Last Post: xSicKxBot
  [Tut] Python Find Longest List in List xSicKxBot 0 1,302 09-23-2022, 02:19 PM
Last Post: xSicKxBot
  [Tut] 5 Pythonic Ways to Print a List without Brackets xSicKxBot 0 1,364 05-25-2022, 10:57 PM
Last Post: xSicKxBot
  [Tut] What’s the Most Pythonic Way to Alias Method Names? xSicKxBot 0 1,272 12-16-2020, 03:20 AM
Last Post: xSicKxBot
  [Tut] How to Convert an Integer List to a Float List in Python xSicKxBot 0 1,328 12-15-2020, 01:03 AM
Last Post: xSicKxBot
  [Tut] How to Reverse/Invert a Dictionary Mapping xSicKxBot 0 1,306 12-11-2020, 02:13 AM
Last Post: xSicKxBot
  [Tut] list.clear() vs New List — Why Clearing a List Rather Than Creating a New One? xSicKxBot 0 1,342 11-30-2020, 06:53 PM
Last Post: xSicKxBot
  [Tut] How to Convert a Float List to an Integer List in Python xSicKxBot 0 1,261 11-11-2020, 06:35 PM
Last Post: xSicKxBot

Forum Jump:


Users browsing this thread:
2 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016