Python | Concatenate N consecutive elements in String list

Sometimes, while working with data, we can have a problem in which we need to perform the concatenation of N consecutive Strings in list of Strings. This can have many applications across domains. Let’s discuss certain ways in which this task can be performed.

Method #1 : Using format() + zip() + iter() + list comprehension
The combination of above methods can be used to perform this particular task. In this we perform the task of grouping using zip() and iter(), format() is used to specify grouping delimiter.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Consecutive N concatenation in String list
# using format() + zip() + iter() + list comprehension
  
# initialize list 
test_list = ['gfg', 'is', 'good', 'for', 'geek', 'people']
  
# printing original list 
print("The original list : " + str(test_list))
  
# initialize N 
N = 3
  
# Consecutive N concatenation in String list
# using format() + zip() + iter() + list comprehension
temp = '{} ' *
res = [temp.format(*ele) for ele in zip(*[iter(test_list)] * N)]
  
# printing result
print("List after N concatenation of String : " + str(res))

chevron_right


Output :



The original list : ['gfg', 'is', 'good', 'for', 'geek', 'people']
List after N concatenation of String : ['gfg is good ', 'for geek people ']

 

Method #2 : Using starmap() + zip() + iter() + format()
The combination of above functions perform the similar task. The only difference is that starmap() is used instead of list comprehension for list construction.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Consecutive N concatenation in String list
# using starmap() + zip() + iter() + format()
from itertools import starmap
  
# initialize list 
test_list = ['gfg', 'is', 'good', 'for', 'geek', 'people']
  
# printing original list 
print("The original list : " + str(test_list))
  
# initialize N 
N = 3
  
# Consecutive N concatenation in String list
# using starmap() + zip() + iter() + format()
temp = '{} ' * N
res = list(starmap(temp.format, zip(*[iter(test_list)] * N)))
  
# printing result
print("List after N concatenation of String : " + str(res))

chevron_right


Output :

The original list : ['gfg', 'is', 'good', 'for', 'geek', 'people']
List after N concatenation of String : ['gfg is good ', 'for geek people ']


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.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.