Python – Group concatenate Till K

Given list of strings, perform concatenation in groups till K.

Input : test_list = [“Gfg”, “is”, “Best”, “”, “I”, “Love”, “”, “Gfg”], K = “”
Output : [‘Gfg is Best’, ‘I Love’, ‘Gfg’]
Explanation : Concatenated words with “” as new String separator.

Input : test_list = [“Gfg”, “is”, “Best”, “”, “I”, “Love”], K = “”
Output : [‘Gfg is Best’, ‘I Love’]
Explanation : Concatenated words with “” as new String separator.

Method : Using loop + join() + list comprehension

This is a way in which this task can be performed. In this, we construct parts of strings into lists, and then perform join of individual lists using join() and list comprehension.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Group concatenate Till K 
# Using loop + join() + list comprehension
  
# initializing lists
test_list = ["Gfg", "is", "Best", None, "I", "Love", None, "Gfg"]
  
# printing original list
print("The original list : " + str(test_list))
  
# initializing K 
K = None
  
# all() to encapsulate whole logic into one expression
res = [[]]
for sub in test_list:
      
    # checking for K value, and performing append to 
    # latest list 
    if sub != K:
        res[-1].append(sub)
    else:
          
        # constructing new list if new group
        res.append([])
  
# Joining all groups 
fin_res = [' '.join(ele) for ele in res]
  
# printing result 
print("Concatenated Groups : " + str(fin_res))

chevron_right


Output

The original list : ['Gfg', 'is', 'Best', None, 'I', 'Love', None, 'Gfg']
Concatenated Groups : ['Gfg is Best', 'I Love', 'Gfg']
My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.