Open In App

Python – Consecutive K elements join in List

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

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




# Python3 code to demonstrate
# Consecutive K elements join in List
# using List comprehension
 
# Initializing list
test_list = ['g', 'f', 'g', 'i', 's', 'b', 'e', 's', 't']
 
# printing original list
print("The original list is : " + str(test_list))
 
# Initializing K
K = 3
 
# Consecutive K elements join in List
# using List comprehension
res = ["".join(test_list[idx: idx + K])
       for idx in range(len(test_list) - K + 1)]
 
# printing result
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




# Python3 code to demonstrate
# Consecutive K elements join in List
# using loop
 
# Initializing list
test_list = ['g', 'f', 'g', 'i', 's', 'b', 'e', 's', 't']
 
# printing original list
print("The original list is : " + str(test_list))
 
# Initializing K
K = 3
 
# Consecutive K elements join in List
# using loop
res = []
for idx in range(0, len(test_list) - K + 1):
    res.append("".join(test_list[idx: idx + K]))
 
# printing result
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)))]
 
 
# Initializing list
test_list = ['g', 'f', 'g', 'i', 's', 'b', 'e', 's', 't']
 
# printing original list
print("The original list is:", test_list)
 
# Initializing K
K = 3
 
# Consecutive K elements join in List
result = consecutive_k_elements_join(test_list, K)
 
# printing result
print("List after consecutive joining:", result)
# This code is contributed by Edula Vinay Kumar Reddy


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

  1. Initialize the list and K variable as shown in the original code.
  2. Define a lambda function that takes a slice of K elements and joins them using the “”.join() method.
  3. 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.
  4. Convert the map object to a list using the list() function.
  5. Print the resulting list.

Python3




# Python3 code to demonstrate
# Consecutive K elements join in List
# using map() function and lambda expression
 
# Initializing list
test_list = ['g', 'f', 'g', 'i', 's', 'b', 'e', 's', 't']
 
# printing original list
print("The original list is : " + str(test_list))
 
# Initializing K
K = 3
 
# Consecutive K elements join in List
# using map() function and lambda expression
res = list(map(lambda x: "".join(x), [
           test_list[i:i+K] for i in range(len(test_list)-K+1)]))
 
# printing result
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.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads