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
# 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)) |
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
# 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]) == N else list1[i : i + N] for i in range(0, len(list1), N)])) # printing result print ("The lists after insertion : " + str(output)) |
Original list : ['p', 'y', 't', 'h', 'o', 'n'] The lists after insertion : ['p', 'y', 't', 'x', 'h', 'o', 'n', 'x']
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Python - Insert character in each duplicate string after every K elements
- Python | Insert after every Nth element in a list
- Python | Ways to sort letters of string alphabetically
- Python | First N letters string construction
- Python program to verify that a string only contains letters, numbers, underscores and dashes
- Python Program that Displays the Letters that are in the First String but not in the Second
- Python program to check if lowercase letters exist in a string
- Python program to calculate the number of digits and letters in a string
- Python | Insert list in another list
- Python | Insert the string at the beginning of all items in a list
- Python | Insert character after every character pair
- Python | Split given list and insert in excel file
- Python regex to find sequences of one upper case letter followed by lower case letters
- Python | Remove all characters except letters and numbers
- Regex in Python to put spaces between words starting with capital letters
- Python Program that Displays Letters that are not common in two strings
- Python | Convert List of String List to String List
- Python program to create a list of tuples from given list having number and its cube in each tuple
- Python | Sort each String in String list
- Python - Remove front K characters from each string in String List
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.

