Skip to content
Related Articles

Related Articles

Python | Triplet iteration in List

View Discussion
Improve Article
Save Article
  • Last Updated : 12 Jun, 2022
View Discussion
Improve Article
Save Article

List iteration is common in programming, but sometimes one requires to print the elements in consecutive triplets. This particular problem is quite common and having a solution to it always turns out to be handy. Lets discuss certain way in which this problem can be solved.

Method #1 : Using list comprehension List comprehension can be used to print the triplets by accessing current, next and next to next element in the list and then printing the same. Care has to be taken while pairing the last elements with the first ones to form a cyclic triplet pairs. 

Python3




# Python3 code to demonstrate
# Triplet iteration in List
# using list comprehension
from itertools import compress
 
# initializing list
test_list = [0, 1, 2, 3, 4, 5]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# using list comprehension
# Triplet iteration in List
res = [((i), (i + 1) % len(test_list), (i + 2) % len(test_list))
        for i in range(len(test_list))]
 
# printing result
print ("The triplet list is : " + str(res))

Output

The original list is : [0, 1, 2, 3, 4, 5]
The triplet list is : [(0, 1, 2), (1, 2, 3), (2, 3, 4), (3, 4, 5), (4, 5, 0), (5, 0, 1)]

Method#2 : Using zip() function zip function can be used to pair the element of the list provided. We generate the list  whose starting element is next element and next to next element. Take care while generating list while merging the last element to first element to form the cyclic triplet.

Python3




# Python3 code to demonstrate
# Triplet iteration in List
# using list comprehension
import itertools
 
# initializing list
l = [0, 1, 2, 3, 4, 5]
# Printing original list
print("The original list is : " + str(l))
# Generating list for pairing
def gen(l, n):
    k1 = itertools.cycle(l);
    k2 = itertools.dropwhile(lambda x: x!=n, k1)
    k3 = itertools.islice(k2, None, len(l))
    return list(k3)
 
# using zip function
# Triplet iteration in List
ans = []
for i in zip(l, gen(l, 1), gen(l, 2)):
    ans.append(tuple(i))
# printing result
print ("The triplet list is : " + str(ans))

Output

The original list is : [0, 1, 2, 3, 4, 5]
The triplet list is : [(0, 1, 2), (1, 2, 3), (2, 3, 4), (3, 4, 5), (4, 5, 0), (5, 0, 1)]

Method#3: Using For loop For loop can be used to print the triplets by accessing current, next and next to next in the list then print the same.  Care has to been taken while pairing the last element with first element when forming cyclic triplet.

Python3




# Python3 code to demonstrate
# Triplet iteration in List
# using list comprehension
 
# initializing list
l = [0, 1, 2, 3, 4, 5]
k = len(l)
# Printing original list
print("The original list is : " + str(l))
 
# using zip function
# Triplet iteration in List
ans = []
for i in range(k):
    x = (l[i], l[(i+1)%k], l[(i+2)%k]);
    ans.append(x)
     
# printing result
print ("The triplet list is : " + str(ans))

Output

The original list is : [0, 1, 2, 3, 4, 5]
The triplet list is : [(0, 1, 2), (1, 2, 3), (2, 3, 4), (3, 4, 5), (4, 5, 0), (5, 0, 1)]

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!