Open In App

Python | Ways to create triplets from given list

Last Updated : 02 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a list of words, write a Python program to create triplets from the given list. 
Examples :

Input: [‘Geeks’, ‘for’, ‘Geeks’, ‘is’, ‘best’, ‘resource’, ‘for’, ‘study’]
 Output: [[‘Geeks’, ‘for’, ‘Geeks’], [‘for’, ‘Geeks’, ‘is’], [‘Geeks’, ‘is’, ‘best’], [‘is’, ‘best’, ‘resource’], [‘best’, ‘resource’, ‘for’], [‘resource’, ‘for’, ‘study’]] 
Input: [‘I’, ‘am’, ‘Paras’, ‘Jain’, ‘I’, ‘Study’, ‘From’, ‘GFG’]
 Output: [[‘I’, ‘am’, ‘Paras’], [‘am’, ‘Paras’, ‘Jain’], [‘Paras’, ‘Jain’, ‘I’], [‘Jain’, ‘I’, ‘Study’], [‘I’, ‘Study’, ‘From’], [‘Study’, ‘From’, ‘GFG’]]

Let’s see some of the methods to do this task. 

Method #1: Using List comprehension 

Python3




# Python code to create triplets from list of words.
 
# List of word initialization
list_of_words = ['I', 'am', 'Paras', 'Jain',
                 'I', 'Study', 'DS', 'Algo']
 
# Using list comprehension
List = [list_of_words[i:i + 3]
        for i in range(len(list_of_words) - 2)]
 
# printing list
print(List)


Output

[['I', 'am', 'Paras'], ['am', 'Paras', 'Jain'], ['Paras', 'Jain', 'I'], ['Jain', 'I', 'Study'], ['I', 'Study', 'DS'], ['Study', 'DS', 'Algo']]

Time Complexity: O(n), where n is the length of the list
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the list 

  Method #2: Using Iteration 

Python3




# Python code to create triplets from list of words.
 
# List of word initialization
list_of_words = ['Geeks', 'for', 'Geeks', 'is',
                 'best', 'resource', 'for', 'study']
 
# Output list initialization
out = []
 
# Finding length of list
length = len(list_of_words)
 
# Using iteration
for z in range(0, length-2):
    # Creating a temp list to add 3 words
    temp = []
    temp.append(list_of_words[z])
    temp.append(list_of_words[z + 1])
    temp.append(list_of_words[z + 2])
    out.append(temp)
 
# printing output
print(out)


Output

[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'], ['Geeks', 'is', 'best'], ['is', 'best', 'resource'], ['best', 'resource', 'for'], ['resource', 'for', 'study']]

Method #3: Using zip function

This approach works by using the zip function to group the elements of the list into tuples of three. The zip function takes multiple iterables as arguments and returns an iterator of tuples, where the i-th tuple contains the i-th element from each of the input iterables. In this case, the input iterables are the original list, and two slices of the list that are shifted by one and two positions respectively.

The resulting iterator is then passed to the list function to create a list of tuples, and a list comprehension is used to convert the tuples to lists.

Python3




# Python code to create triplets from list of words.
 
# List of word initialization
list_of_words = ['Geeks', 'for', 'Geeks', 'is',
                 'best', 'resource', 'for', 'study']
triplets = [list(t) for t in zip(list_of_words, list_of_words[1:], list_of_words[2:])]
# printing list
 
print(triplets)
#This code is contributed by Edula Vinay Kumar Reddy


Output

[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'], ['Geeks', 'is', 'best'], ['is', 'best', 'resource'], ['best', 'resource', 'for'], ['resource', 'for', 'study']]

Time complexity: O(n), where n is the number of elements in the list. This is because the zip function processes the elements of the list in a single pass, and the list comprehension also processes the elements in a single pass.
 Auxiliary space: O(n), since a new list is created to store the triplets. The original list is not modified.

Using for loop and slicing in python:

Approach:

Define a function create_triplets that takes a list lst as input.
Initialize an empty list triplets to store the triplets.
Use a for loop to iterate over the range from 0 to the length of the list minus 2 (since we need at least 3 elements to form a triplet).
Within the loop, create a triplet by slicing the list from the current index to the next two indices.
Append the triplet to the triplets list.
Return the triplets list.
Create two input lists input1 and input2.
Call the create_triplets function for each input and store the results in output1 and output2.
Print the output1 and output2 lists.

Python3




def create_triplets(lst):
    triplets = []
    for i in range(len(lst)-2):
        triplet = lst[i:i+3]
        triplets.append(triplet)
    return triplets
 
input1 = ['Geeks', 'for', 'Geeks', 'is', 'best', 'resource', 'for', 'study']
output1 = create_triplets(input1)
print(output1)
 
input2 = ['I', 'am', 'Paras', 'Jain', 'I', 'Study', 'From', 'GFG']
output2 = create_triplets(input2)
print(output2)


Output

[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'], ['Geeks', 'is', 'best'], ['is', 'best', 'resource'], ['best', 'resource', 'for'], ['resource', 'for', 'study']]
[['I', 'am', 'Paras'], ['am', 'Paras', 'Jain'], ['Paras', 'Jain', 'I'], ['Jain', 'I', 'Study'], ['I', 'Study', 'From'], ['Study', 'From', 'GFG']]

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads