Open In App

Python | Consecutive elements pairing in list

Last Updated : 24 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

This process involves creating pairs of elements that appear next to each other, which can be invaluable for various applications such as data analysis, pattern recognition and algorithm development. We can achieve this using different methods in Python, such as using simple loops, list slicing, zip() or itertools.pairwise().

Using itertools.pairwise()

The most efficient and cleanest method to pair consecutive elements is by using the itertools.pairwise() function. This function is part of the Python standard library. It’s ideal for larger lists because it’s fast and concise. The itertools.pairwise() function automatically takes a list and creates pairs of consecutive elements, without needing any extra code to loop through or slice the list.

Python
import itertools

# Initialize list
a = [10, 20, 30, 40, 50]

# Pair consecutive elements using itertools.pairwise()
p = list(itertools.pairwise(a))

# Print the pairs
print(p)

Output

[(10, 20), (20, 30), (30, 40), (40, 50)]

Other methods that we can use to pairing consecutive elements in a list are:

Using the zip() Function

The zip() function is another great built-in Python tool for pairing consecutive elements. It works by taking two sequences and combining them into pairs, element by element. To pair consecutive elements, we can combine the original list with a sliced version of the list that starts from the second element.

Python
# Initialize list
a = [10, 20, 30, 40, 50]

# Pair consecutive elements using zip
p = list(zip(a, a[1:]))

# Print the pairs
print(p)

Output
[(10, 20), (20, 30), (30, 40), (40, 50)]
  • Here, zip(a, a[1:]) takes the original list a and pairs each element with the one that comes immediately after it. a[1:] is a slice of the list starting from the second element.

Using List Comprehension and Slicing

List comprehension allows us to pair consecutive elements in a single line of code by iterating over the list and accessing each element and its next element through indexing. We use the range(len(a) – 1) to loop through the indices of the list a and for each index i, we create a pair consisting of the element at index i and the element at index i + 1.

Python
# Initialize list
a = [10, 20, 30, 40, 50]

# Create pairs using list comprehension
p = [(a[i], a[i+1]) for i in range(len(a) - 1)]

# Print the pairs
print(p)

Output
[(10, 20), (20, 30), (30, 40), (40, 50)]

Using a Simple Loop

A simple for loop is one of the most straightforward ways to pair consecutive elements. We manually loop through the list and create pairs by accessing the current element and the next element in each iteration.

Python
# Initialize list
a = [10, 20, 30, 40, 50]

# Loop through the list to pair consecutive elements
p = []
for i in range(len(a) - 1):
    p.append((a[i], a[i + 1]))

# Print the pairs
print(p)

Output
[(10, 20), (20, 30), (30, 40), (40, 50)]

Using map() and lambda

map() function applies a given function to all items in an iterable (like a list). We can use it with a lambda function to pair consecutive elements. However, this method is a bit more complex and less readable compared to other methods, so it is typically used in more advanced scenarios. The map() function takes two iterables and applies the lambda function to each pair of elements.

Python
# Initialize list
a = [10, 20, 30, 40, 50]

# Pair consecutive elements using map and lambda
p = list(map(lambda x, y: (x, y), a, a[1:]))

# Print the pairs
print(p)

Output
[(10, 20), (20, 30), (30, 40), (40, 50)]


Next Article

Similar Reads