![]() |
|
[Tut] The Most Pythonic Way to Join a List in Reverse Order - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] The Most Pythonic Way to Join a List in Reverse Order (/thread-95629.html) |
[Tut] The Most Pythonic Way to Join a List in Reverse Order - xSicKxBot - 06-11-2020 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 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:
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?
You can see this in our interactive memory visualizer: https://pythontutor.com/iframe-embed.html#code=l%20%3D%20%5B'n',%20'o',%20'h',%20't',%20'y',%20'p'%5D%0As%20%3D%20''%0Afor%20x%20in%20reversed%28l%29%3A%0A%20%20%20%20s%20%2B%3D%20x%0Aprint%28s%29%0A&codeDivHeight=400&codeDivWidth=350&cumulative=false&curInstr=0&heapPrimitives=nevernest&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=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! |