Open In App

Python – Consecutive Triple element pairing

Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with lists, we need to triple pair up the like elements in list and then store them as lists of list. This particular task has it’s 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, (i+1)th and (i+2)th element. 

Python3




# Python3 code to demonstrate
# Consecutive Triple 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 Triple element pairing
res = [[test_list[i], test_list[i + 1], test_list[i + 2]] for i in range(len(test_list) - 2)]
 
# print result
print("The consecutive element triple paired list is : " + str(res))


Output : 

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

Time Complexity: O(n*n) where n is the number of elements in the list “test_list”. The list comprehension is used to perform the task and it takes O(n*n) time.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the list “test_list”.

Method #2 : Using zip() + map() This task can also be achieved using the zip function which performs the task for all the elements and map function does the task of pairing of consecutive elements. 

Python3




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


Output : 

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

Time Complexity: O(n*n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.

Method #3: Using a for loop and slicing.

Step-by-step approach:

  • Initialize an empty list called “res” to store the consecutive triple element pairing.
  • Use a for loop to iterate over the list starting from the first element till the third last element (i.e., len(test_list) – 2).
  • In each iteration, slice the list from the current index to the next two indices (i.e., test_list[i:i+3]) and append the sliced list to the “res” list.
  • Print the “res” list to show the consecutive triple element pairing.

Below is the implementation of the above approach:

Python3




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


Output

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

Time complexity: O(n), where n is the length of the list, because we are iterating over the list only once. 
Auxiliary space: O(n), because we are creating a new list “res” to store the consecutive triple element pairing.



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