Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python - Ways to create triplets from given list
A triplet is a group of three consecutive elements from a list. Creating triplets from a given list means extracting all possible combinations of three adjacent elements. Python provides several approaches to accomplish this task efficiently.
Using List Comprehension
List comprehension provides a concise way to create triplets by slicing the list ?
# List of words
words = ['I', 'am', 'Vishesh', 'and', 'I', 'like', 'Python', 'programming']
# Using list comprehension to create triplets
triplets = [words[i:i + 3] for i in range(len(words) - 2)]
print("Triplets using list comprehension:")
print(triplets)
Triplets using list comprehension: [['I', 'am', 'Vishesh'], ['am', 'Vishesh', 'and'], ['Vishesh', 'and', 'I'], ['and', 'I', 'like'], ['I', 'like', 'Python'], ['like', 'Python', 'programming']]
Using For Loop with Slicing
A traditional approach using a for loop to iterate and slice the list ?
# List of words
words = ['I', 'am', 'Vishesh', 'and', 'I', 'like', 'Python', 'programming']
# Using for loop with slicing
triplets = []
for i in range(len(words) - 2):
triplets.append(words[i:i + 3])
print("Triplets using for loop with slicing:")
print(triplets)
Triplets using for loop with slicing: [['I', 'am', 'Vishesh'], ['am', 'Vishesh', 'and'], ['Vishesh', 'and', 'I'], ['and', 'I', 'like'], ['I', 'like', 'Python'], ['like', 'Python', 'programming']]
Using For Loop with Individual Elements
Manually creating each triplet by accessing individual elements ?
# List of words
words = ['I', 'am', 'Vishesh', 'and', 'I', 'like', 'Python', 'programming']
# Using for loop with individual elements
triplets = []
for i in range(len(words) - 2):
temp = []
temp.append(words[i])
temp.append(words[i + 1])
temp.append(words[i + 2])
triplets.append(temp)
print("Triplets using individual elements:")
print(triplets)
Triplets using individual elements: [['I', 'am', 'Vishesh'], ['am', 'Vishesh', 'and'], ['Vishesh', 'and', 'I'], ['and', 'I', 'like'], ['I', 'like', 'Python'], ['like', 'Python', 'programming']]
Using zip() Function
The zip() function can combine three shifted versions of the same list ?
# List of words
words = ['I', 'am', 'Vishesh', 'and', 'I', 'like', 'Python', 'programming']
# Using zip function
triplets = list(zip(words, words[1:], words[2:]))
print("Triplets using zip function:")
print([list(triplet) for triplet in triplets])
Triplets using zip function: [['I', 'am', 'Vishesh'], ['am', 'Vishesh', 'and'], ['Vishesh', 'and', 'I'], ['and', 'I', 'like'], ['I', 'like', 'Python'], ['like', 'Python', 'programming']]
Comparison
| Method | Readability | Performance | Best For |
|---|---|---|---|
| List Comprehension | High | Good | Concise code |
| For Loop with Slicing | Medium | Good | Clear logic flow |
| Individual Elements | Low | Average | Learning purposes |
| zip() Function | High | Excellent | Functional programming |
Conclusion
List comprehension and zip() function are the most efficient methods for creating triplets. Use list comprehension for readable code and zip() for optimal performance in functional programming style.
