Python | Insert value after each k letters in given list of string

Given a list of string, write a Python program to Insert some letter after each k letters in given list of strings.

As we know inserting element in a list is quite common, but sometimes we need to operate on list of strings by considering each letter and insert some repeated elements some fixed frequency. Let’s see how to achieve this task using Python.

Method #1: Using enumerate() method

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to insert value after
# each k letters in given list of string
list1 = ['p', 'y', 't', 'h', 'o', 'n'
  
# printing original list 
print ("Original list : " + str(list1)) 
  
# initializing k 
k = 'G'
  
# initializing N 
N = 2
  
# using join() + enumerate() 
# inserting K after every Nth number 
output = list(''.join(i + k * (N % 2 == 1
        for N, i in enumerate(list1))) 
  
# printing result 
print ("The lists after insertion : " + str(output)) 

chevron_right


Output:

Original list : ['p', 'y', 't', 'h', 'o', 'n']
The lists after insertion : ['p', 'y', 'G', 't', 'h', 'G', 'o', 'n', 'G']

 

Method #2: Using itertools

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to insert value after
# each k letters in given list of string
from itertools import chain 
  
list1 = ['p', 'y', 't', 'h', 'o', 'n'
  
# printing original list 
print ("Original list : " + str(list1)) 
  
# initializing k  
k = 'x'
    
# initializing N 
N = 3
    
  
# inserting K after every Nth number  
output = list(chain(*[list1[i : i + N] + [k]  
              if len(list1[i : i + N]) == else list1[i : i + N]  
              for i in range(0, len(list1), N)])) 
  
# printing result 
print ("The lists after insertion : " + str(output)) 

chevron_right


Output:

Original list : ['p', 'y', 't', 'h', 'o', 'n']
The lists after insertion : ['p', 'y', 't', 'x', 'h', 'o', 'n', 'x']


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.