![]() |
|
[Tut] Python Join List Pairs - 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] Python Join List Pairs (/thread-95671.html) |
[Tut] Python Join List Pairs - xSicKxBot - 06-13-2020 Python Join List Pairs Given a list of strings. Join the first with the second string, the second with the third, and so on. The one-liner Code: [lst[i] + lst[i+1] for i in range(0, len(lst), 2)]Code: i=0, 2, 4, 5, ...Code: iCode: i+1Code: lst[i] + lst[i+1]You may already know the normal join function in Python: Intro: Python Join Problem: Given a listof elements. How to join the elements by concatenating all elements in the list? Example: You want to convert list Code: ['learn ', 'python ', 'fast']Code: 'learn python fast'Quick Solution: to convert a list of strings to a string, do the following.
Code: Let’s have a look at the code. Code: lst = ['learn ', 'python ', 'fast']
print(''.join(lst))The output is: Code: learn python fastHowever, what if you want to do something different. Rather than joining all strings in the list to a single string, you want to join the strings in the list in pairs. Problem: Python Join List Pairs Problem: Given a list of strings. Join the first with the second string, the second with the third, and so on. Example: Let’s consider the following minimal example: Code: ['x', 'y', 'v', 'w']Is there any simple way to pair the first with the second and the third with the fourth string to obtain the following output? Code: ['xy', 'vw']Note that the length of the strings in the list is variable so the following would be a perfectly acceptable input: Code: ['aaaa', 'b', 'cc', 'dddd', 'eee', 'fff']You can play with all three methods before diving into each of them: https://trinket.io/embed/python/1a68a6e75f Exercise: What’s the most Pythonic method? Method 1: Zip() + List Comprehension You can use the following smart one-liner solution Code: lst = ['aaaa', 'b', 'cc', 'dddd', 'eee', 'fff']
out = [x + y for x,y in zip(lst[::2], lst[1::2])]
print(out)
# ['aaaab', 'ccdddd', 'eeefff']The one-liner uses the following strategy:
Method 2: Iterator + List Comprehension You can also use an iterator to accomplish this: Code: lst = ['aaaa', 'b', 'cc', 'dddd', 'eee', 'fff']
it = iter(lst)
out = [x + next(it, '') for x in it] print(out)
# ['aaaab', 'ccdddd', 'eeefff']Here’s the idea:
Method 3: Use List Comprehension with Indexing This method is the most straightforward one for Python beginners: Code: lst = ['aaaa', 'b', 'cc', 'dddd', 'eee', 'fff']
out = [lst[i] + lst[i+1] for i in range(0, len(lst), 2)]
print(out)
# ['aaaab', 'ccdddd', 'eeefff']The idea is simply to use the rangefunction to iterate over every other index Code: i=0, 2, 4, 5, ...Code: iCode: i+1Code: lst[i] + lst[i+1]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! |