06-13-2020, 02:51 PM
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 solves the problem by using the range function to iterate over every other index to concatenate the -th and the -th elements in a list comprehension expression with .
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 to the string .
Quick Solution: to convert a list of strings to a string, do the following.
Code: Let’s have a look at the code.
The output is:
However, 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:
Is there any simple way to pair the first with the second and the third with the fourth string to obtain the following output?
Note that the length of the strings in the list is variable so the following would be a perfectly acceptable input:
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
The one-liner uses the following strategy:
Method 2: Iterator + List Comprehension
You can also use an iterator to accomplish this:
Here’s the idea:
Method 3: Use List Comprehension with Indexing
This method is the most straightforward one for Python beginners:
The idea is simply to use the rangefunction to iterate over every other index to access the -th and the -th elements at the same time in the expression statement of list comprehension (to concatenatethose with ).
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!
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.
- Call the method on the empty stringCode:
''.join(list)that glues together all strings in theCode:''and returns a new string.Code:list
- The string on which you call the join method is used as a delimiter between the list elements.
- If you don’t need a delimiter, just use the empty string .Code:
''
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:
- Obtain two slices andCode:
lst[::2]of the original list over every other element starting from the first and the second elements, respectively. If you need to refresh your slicing skills, check out my detailed blog article.Code:lst[1::2]
- Zip the two slices to a sequence of tuples using the function. This aligns the first with the second elements from the original list, the third with the forth, and so on. To refresh yourCode:
zip(...)skills, check out my blog tutorial here.Code:zip()
- Use list comprehension to iterate over each pair of values and concatenate them using list concatenationCode:
x,y. For a refresher on list comprehension, check out this free tutorial—and for a refresher on list concatenation, check out this one.Code:x+y
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:
- Create an iterator object it using the built-in function .Code:
iter()
- Use list comprehension to go over each element in the iterator.
- Concatenate each element with the return value of calling the function on the iterator. This ensures that the iterator moves one position further iterating over the list. So, the next elementCode:
next()won’t be a duplicate.Code:x
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!

