Python | Pair iteration in list
List iteration is common in Python programming, but sometimes one requires to print the elements in consecutive pairs. This particular problem is quite common and having a solution to it always turns out to be handy. Let’s discuss certain ways in which this problem can be solved.
Pair iteration in a list using list comprehension
List comprehension can be used to print the pairs by accessing the current and next elements in the list and then printing the same. Care has to be taken while pairing the last element with the first one to form a cyclic pair.
Python3
from itertools import compress# initializing listtest_list = [0, 1, 2, 3, 4, 5]# printing original listprint ("The original list is : " + str(test_list))# using list comprehension# to perform pair iteration in listres = [((i), (i + 1) % len(test_list)) for i in range(len(test_list))]# printing resultprint ("The pair list is : " + str(res)) |
Output:
The original list is : [0, 1, 2, 3, 4, 5] The pair list is : [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 0)]
Time complexity: O(n), where n is the length of the test_list.
Auxiliary space: O(n), where n is the length of the test_list.
Pair iteration in a list using zip() + list slicing
The zip function can be used to extract pairs over the list slicing can be used to successively pair the current element with the next one for efficient pairing.
Python3
from itertools import compress# initializing listtest_list = [0, 1, 2, 3, 4, 5]# printing original listprint ("The original list is : " + str(test_list))# using zip() + list slicing# to perform pair iteration in listres = list(zip(test_list, test_list[1:] + test_list[:1]))# printing resultprint ("The pair list is : " + str(res)) |
Output:
The original list is : [0, 1, 2, 3, 4, 5] The pair list is : [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 0)]
Time complexity: O(n), where n is the length of the input list test_list.
Auxiliary space: O(n), where n is the length of the input list test_list.
Pair iteration in a list using lambda and itertools:
One approach to iterating over pairs in a list using a lambda function could be as follows:
Python3
from itertools import teedef pairwise(iterable): a, b = tee(iterable) next(b, None) return zip(a, b)test_list = [0, 1, 2, 3, 4, 5]result = list(map(lambda x: (x[0], x[1]), pairwise(test_list)))print(result)#This code is contributed by Edula Vinay Kumar Reddy |
[(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)]
Time complexity: O(n) as it requires a single pass through the iterable to create the pairs.
Auxiliary space: O(n) as it requires storing the pairs in a list.
Pair iteration in a list using a for loop:
Python3
test_list = [0, 1, 2, 3, 4, 5]# printing original listprint ("The original list is : " + str(test_list))res = []for i in range(len(test_list)): res.append((test_list[i], test_list[(i + 1) % len(test_list)]))print("The pair list is :", res)#This code is contributed by Jyothi pinjala. |
The original list is : [0, 1, 2, 3, 4, 5] The pair list is : [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 0)]
Time Complexity: O(n)
Auxiliary Space: O(n)






Please Login to comment...