Python | Consecutive elements pairing in list
Last Updated :
24 Dec, 2024
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)]
Similar Reads
Python - Consecutive K elements join in List Sometimes, while working with Python lists, we can have a problem in which we need to join every K character into one collection. This type of application can have use cases in many domains like day-day and competitive programming. Let us discuss certain ways in which this task can be performed. Met
4 min read
Python | Consecutive Pair Minimums Sometimes, while working with Python list, one can have a problem in which one needs to find perform the minimum of list in pair form. This is useful as a subproblem solution of bigger problem in web development and day-day programming. Letâs discuss certain ways in which this problem can be solved.
4 min read
Python - Filter consecutive elements Tuples Given a Tuple list, filter tuples that are made from consecutive elements, i.e diff is 1. Input : test_list = [(3, 4, 5, 6), (5, 6, 7, 2), (1, 2, 4), (6, 4, 6, 3)] Output : [(3, 4, 5, 6)] Explanation : Only 1 tuple adheres to condition. Input : test_list = [(3, 4, 5, 6), (5, 6, 7, 2), (1, 2, 3), (6,
5 min read
Python | Concatenate N consecutive elements in String list Sometimes, while working with data, we can have a problem in which we need to perform the concatenation of N consecutive Strings in a list of Strings. This can have many applications across domains. Let's discuss certain ways in which this task can be performed. Method #1: Using format() + zip() + i
8 min read
Python - Consecutive identical elements count Given the elements list, get all the elements that have an identical element as the next element. Input : test_list = [4, 5, 5, 5, 5, 6, 6, 7, 8, 2, 2, 10] Output : 3 Explanation : 5, 6 and 2 has identical element as their next element. Input : test_list = [4, 5, 5, 5, 5, 6, 6, 7, 8, 2, 3, 10] Outpu
5 min read