Python | Ways to join pair of elements in list
Given a list, the task is to join a pair of elements of the list. Given below are a few methods to solve the given task.
Method #1: Using zip() method
# Python code to demonstrate # how to join pair of elements of list # Initialising list ini_list = ['a', 'b', 'c', 'd', 'e', 'f'] # Printing initial list print ("Initial list", str(ini_list)) # Pairing the elements of lists res = [i + j for i, j in zip(ini_list[::2], ini_list[1::2])] # Printing final result print ("Result", str(res)) |
chevron_right
filter_none
Output:
Initial list ['a', 'b', 'c', 'd', 'e', 'f'] Result ['ab', 'cd', 'ef']
Method #2: Using list comprehension and next and iters
# Python code to demonstrate # how to join pair of elements of list # Initialising list ini_list = iter(['a', 'b', 'c', 'd', 'e', 'f']) # Pairing the elements of lists res = [h + next(ini_list, '') for h in ini_list] # Printing final result print ("Result", str(res)) |
chevron_right
filter_none
Output:
Result ['ab', 'cd', 'ef']
Method #3: Using list comprehension
# Python code to demonstrate # how to join pair of elements of list # Initialising list ini_list = ['a', 'b', 'c', 'd', 'e', 'f'] # Printing initial lists print ("Initial list", str(ini_list)) # Pairing the elements of lists res = [ini_list[i] + ini_list[i + 1] for i in range(0, len(ini_list), 2)] # Printing final result print ("Result", str(res)) |
chevron_right
filter_none
Output:
Initial list ['a', 'b', 'c', 'd', 'e', 'f'] Result ['ab', 'cd', 'ef']
Recommended Posts:
- Python | Pandas str.join() to join string/list elements with passed delimiter
- Python | Join tuple elements in a list
- Python | Pair summation of list elements
- Python | Make pair from two list such that elements are not same in pairs
- Python | Ways to format elements of given list
- Python | Join cycle in list
- Python | Pair and combine nested list to tuple list
- Python | Ways to sum list of lists and return sum list
- Python | Ways to Convert a 3D list into a 2D list
- Python | Pair iteration in list
- Python List Comprehension to find pair with given sum from two arrays
- Python | Ways to Spilt the list by some value
- Different ways to clear a list in Python
- Python | Ways to flatten a 2D list
- Python | Ways to rotate a list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



