Join pairs of list elements in Python

avatar

Borislav Hadzhiev

Last updated: Jun 18, 2022

banner

Photo from Unsplash

Join pairs of list elements in Python #

To join pairs of list elements in Python:

  1. Use list slicing with steps to get every second element starting at index 0 and 1.
  2. Use the zip() function to iterate over the two lists.
  3. Use the addition operator (+) to join the list items.
main.py
my_list = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'] # 👇️ get every second element starting at index 0 print(my_list[::2]) # 👉️ ['A', 'C', 'E', 'G'] # 👇️ get every second element starting at index 1 print(my_list[1::2]) # 👉️ ['B', 'D', 'F', 'H'] result = [item_1 + item_2 for item_1, item_2 in zip(my_list[::2], my_list[1::2])] print(result) # 👉️ ['AB', 'CD', 'EF', 'GH']

The syntax for list slicing is my_list[start:stop:step].

The slice of the list [::2] selects every second element starting at index 0.

The second slice [1::2] selects every second element starting at index 1.

We used the zip() function to get a list of tuples that contain an element from each list.

main.py
my_list = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'] # 👇️ get every second element starting at index 0 print(my_list[::2]) # 👉️ ['A', 'C', 'E', 'G'] # 👇️ get every second element starting at index 1 print(my_list[1::2]) # 👉️ ['B', 'D', 'F', 'H'] # 👇️ [('A', 'B'), ('C', 'D'), ('E', 'F'), ('G', 'H')] print(list(zip(my_list[::2], my_list[1::2])))

The zip function iterates over several iterables in parallel and produces tuples with an item from each iterable.

The final step is to use a list comprehension to iterate over the list of tuples and join the elements in the tuple.

main.py
my_list = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'] # 👇️ get every second element starting at index 0 print(my_list[::2]) # 👉️ ['A', 'C', 'E', 'G'] # 👇️ get every second element starting at index 1 print(my_list[1::2]) # 👉️ ['B', 'D', 'F', 'H'] # 👇️ [('A', 'B'), ('C', 'D'), ('E', 'F'), ('G', 'H')] print(list(zip(my_list[::2], my_list[1::2]))) result = [item_1 + item_2 for item_1, item_2 in zip(my_list[::2], my_list[1::2])] print(result) # 👉️ ['AB', 'CD', 'EF', 'GH']

List comprehensions are used to perform some operation for every element, or select a subset of elements that meet a condition.

Alternatively, you can use the range() class.

To join pairs of list elements in Python:

  1. Use the range() class with a step argument or 2.
  2. Use a list comprehension to iterate over the range of indexes.
  3. On each iteration, join the list item at the current index with the item at the next index.
main.py
my_list = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'] result = [ my_list[idx] + my_list[idx + 1] for idx in range(0, len(my_list), 2) ] print(result) # 👉️ ['AB', 'CD', 'EF', 'GH']

The range class is commonly used for looping a specific number of times in for loops and takes the following parameters:

NameDescription
startAn integer representing the start of the range (defaults to 0)
stopGo up to, but not including the provided integer
stepRange will consist of every N numbers from start to stop (defaults to 1)

The example creates a range containing every second index starting at 0.

main.py
my_list = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'] # 👇️ [0, 2, 4, 6] print(list(range(0, len(my_list), 2)))

On each iteration, we use the addition operator to join the element at the current index and the element at the next index in the list.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.