Open In App

Python | Consecutive elements pairing in list

Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with lists, we need to pair up the like elements in the list and then store them as lists of lists. This particular task has its utility in many domains, be it web development or day-day programming. Let’s discuss certain ways in which this can be achieved. 

Method #1: Using list comprehension

The list comprehension can be easily used to perform this particular task, but consecutively making the pairs of i’th and (i+1)th element.

Python3




# Python3 code to demonstrate
# consecutive element pairing
# using list comprehension
 
# initializing list
test_list = [5, 4, 1, 3, 2]
 
# printing original list
print("The original list : " + str(test_list))
 
# using list comprehension
# consecutive element pairing
res = [[test_list[i], test_list[i + 1]]
       for i in range(len(test_list) - 1)]
 
# print result
print("The consecutive element paired list is : " + str(res))


Output : 

    
The original list : [5, 4, 1, 3, 2]
The consecutive element paired list is : [[5, 4], [4, 1], [1, 3], [3, 2]]

Method # 2: Using zip() 

This task can also be achieved using only the zip function which performs the task for all the elements. 

Python3




# Python3 code to demonstrate
# consecutive element pairing
# using zip()
 
# initializing list
test_list = [5, 4, 1, 3, 2]
 
# printing original list
print("The original list : " + str(test_list))
 
# using zip()
# consecutive element pairing
res = list(zip(test_list, test_list[1:]))
 
# print result
print("The consecutive element paired list is : " + str(res))


Output : 

    
The original list : [5, 4, 1, 3, 2]
The consecutive element paired list is : [[5, 4], [4, 1], [1, 3], [3, 2]]

The time complexity of this approach is O(n), where n is the length of the input list, because we are only iterating over the input list once to create a new list using the zip function.

The auxiliary space used by this approach is O(n), because we are creating a new list to store the consecutive element pairs.

Method # 3: Using itertools

Here is an approach using the itertools.tee function from the itertools module to pair consecutive elements in a list:

Python3




import itertools
 
# initializing list
test_list = [5, 4, 1, 3, 2]
 
# printing original list
print("The original list :", test_list)
 
# use itertools.tee to create two iterators from the list
a, b = itertools.tee(test_list)
 
# advance the iterator by one element
next(b, None)
 
# use zip to pair the elements from the two iterators
res = list(zip(a, b))
 
# print result
print("The consecutive element paired list is :", res)
# This code is contributed by Edula Vinay Kumar Reddy


Output

The original list : [5, 4, 1, 3, 2]
The consecutive element paired list is : [(5, 4), (4, 1), (1, 3), (3, 2)]

Time complexity: O(n)
Auxiliary Space: O(n)

Method#4: Using Recursive method

Python3




def pair_consecutive_recursive(input_list, index=0):
    if index == len(input_list) - 1:
        return []
    return [(input_list[index], input_list[index + 1])] + pair_consecutive_recursive(input_list, index + 1)
 
 
# initializing list
test_list = [5, 4, 1, 3, 2]
 
# printing original list
print("The original list:", test_list)
 
# using the recursive function to pair consecutive elements
result = pair_consecutive_recursive(test_list)
 
# printing result
print("The consecutive element paired list is:", result)
 
# this code contributed by tvsk


Output

The original list: [5, 4, 1, 3, 2]
The consecutive element paired list is: [(5, 4), (4, 1), (1, 3), (3, 2)]

Time Complexity: O(n)
Auxiliary Space: O(n)

Method 5: Using for loop 

Here we will be iiterating through the list and append pairs of consecutive elements to a new list.

This program takes a list of integers and pairs consecutive elements of the list. The pairs are then stored in a new list called res. The program then prints the original list and the list of paired consecutive elements.

The first line initializes a list of integers, test_list, which is the input to the program. The second line prints the original list to the console. The third and fourth lines use a for loop to iterate over the list and append pairs of consecutive elements to the res list. The fifth line prints the final list of consecutive element pairs to the console.

Python3




# Python3 code to demonstrate
# consecutive element pairing
# using a for loop
 
# initializing list
test_list = [5, 4, 1, 3, 2]
 
# printing original list
print("The original list : " + str(test_list))
 
# using a for loop
# consecutive element pairing
res = []
for i in range(len(test_list) - 1):
    res.append([test_list[i], test_list[i + 1]])
 
# print result
print("The consecutive element paired list is : " + str(res))


Output

The original list : [5, 4, 1, 3, 2]
The consecutive element paired list is : [[5, 4], [4, 1], [1, 3], [3, 2]]

Time complexity: O(n), where n is the length of the input list test_list. 
Auxiliary space: O(n), since the output list res will have n/2 pairs of elements, each taking up constant space.

Method 5: Using numpy array slicing

Approach:

  1. Import the numpy module.
  2. Convert the given list to a numpy array.
  3. Use array slicing to create two slices of the array, one starting from the first element and the other starting from the second element.
  4. Stack these two slices horizontally using the column_stack() method of numpy.
  5. The resulting array contains pairs of consecutive elements.

Python3




import numpy as np
 
# initializing list
test_list = [5, 4, 1, 3, 2]
 
# converting list to numpy array
arr = np.array(test_list)
 
# using array slicing
res = np.column_stack((arr[:-1], arr[1:]))
 
# printing result
print("The consecutive element paired list is : " + str(res))


Output:

The consecutive element paired list is :[[5 4][4 1][1 3][3 2]]

Time complexity: O(n)
Auxiliary space: O(n)



Last Updated : 13 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads