Python – Consecutive K elements join in List
Sometimes, while working with Python lists, we can have a problem in which we need to join every K character into one collection. This type of application can have use cases in many domains like day-day and competitive programming. Let us discuss certain ways in which this task can be performed.
Method #1: Using List comprehension
This is one of the ways by which this task can be performed. In this, we iterate through the list and join elements using list slicing and return the aggregated list.
Python3
test_list = [ 'g' , 'f' , 'g' , 'i' , 's' , 'b' , 'e' , 's' , 't' ]
print ( "The original list is : " + str (test_list))
K = 3
res = ["".join(test_list[idx: idx + K])
for idx in range ( len (test_list) - K + 1 )]
print ( "List after consecutive joining : " + str (res))
|
Output
The original list is : ['g', 'f', 'g', 'i', 's', 'b', 'e', 's', 't']
List after consecutive joining : ['gfg', 'fgi', 'gis', 'isb', 'sbe', 'bes', 'est']
Time Complexity: O(n), where n is the length of the input list. This is because we’re using list comprehension which has a time complexity of O(n) in the worst case.
Auxiliary Space: O(n), as we’re using additional space res other than the input list itself with the same size of input list.
Method #2: Using loop
This is a brute way to perform this task. This is similar to the above method, just the string is iterated using a loop and making task more longer and tedious.
Python3
test_list = [ 'g' , 'f' , 'g' , 'i' , 's' , 'b' , 'e' , 's' , 't' ]
print ( "The original list is : " + str (test_list))
K = 3
res = []
for idx in range ( 0 , len (test_list) - K + 1 ):
res.append("".join(test_list[idx: idx + K]))
print ( "List after consecutive joining : " + str (res))
|
Output
The original list is : ['g', 'f', 'g', 'i', 's', 'b', 'e', 's', 't']
List after consecutive joining : ['gfg', 'fgi', 'gis', 'isb', 'sbe', 'bes', 'est']
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3: Using itertools
Here’s a solution using the islice function from the itertools module and the join method of strings.
Python3
from itertools import islice
def consecutive_k_elements_join(lst, k):
return [''.join(x) for x in zip ( * (islice(lst, i, None ) for i in range (k)))]
test_list = [ 'g' , 'f' , 'g' , 'i' , 's' , 'b' , 'e' , 's' , 't' ]
print ( "The original list is:" , test_list)
K = 3
result = consecutive_k_elements_join(test_list, K)
print ( "List after consecutive joining:" , result)
|
Output
The original list is: ['g', 'f', 'g', 'i', 's', 'b', 'e', 's', 't']
List after consecutive joining: ['gfg', 'fgi', 'gis', 'isb', 'sbe', 'bes', 'est']
This solution has a time complexity of O(n) and a space complexity of O(k), where n is the length of lst and k is the value of K.
Method 4 : Using the map() function and lambda expression
- Initialize the list and K variable as shown in the original code.
- Define a lambda function that takes a slice of K elements and joins them using the “”.join() method.
- Use the map() function to apply the lambda function to each slice of K consecutive elements in the list. This will give you a map object.
- Convert the map object to a list using the list() function.
- Print the resulting list.
Python3
test_list = [ 'g' , 'f' , 'g' , 'i' , 's' , 'b' , 'e' , 's' , 't' ]
print ( "The original list is : " + str (test_list))
K = 3
res = list ( map ( lambda x: "".join(x), [
test_list[i:i + K] for i in range ( len (test_list) - K + 1 )]))
print ( "List after consecutive joining : " + str (res))
|
Output
The original list is : ['g', 'f', 'g', 'i', 's', 'b', 'e', 's', 't']
List after consecutive joining : ['gfg', 'fgi', 'gis', 'isb', 'sbe', 'bes', 'est']
Time Complexity: O(nK) – where n is the length of the list and K is the size of the slice.
Auxiliary Space: O(nK) – since we are creating a new list to store the slices.
Last Updated :
08 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...