Python | Insert after every Nth element in a list
Sometimes we need to perform a single insertion in python, this can be easily done with the help of the insert function. But sometimes, we require to insert in a repeated manner after every n numbers, for that there can be numerous shorthands that can be quite handy. Lets discuss certain ways in which this can be done.
Method #1 : Using join() + enumerate()
We can use the join function to join each of nth substring with the digit K and enumerate can do the task of performing the selective iteration of list.
# Python3 code to demonstrate # inserting K after every Nth number # using join() + enumerate() # initializing list test_list = ['g', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's'] # printing original list print ("The original list is : " + str(test_list)) # initializing k k = 'x' # initializing N N = 3 # using join() + enumerate() # inserting K after every Nth number res = list(''.join(i + k * (N % 3 == 2) for N, i in enumerate(test_list))) # printing result print ("The lists after insertion : " + str(res)) |
The original list is : [‘g’, ‘e’, ‘e’, ‘k’, ‘s’, ‘f’, ‘o’, ‘r’, ‘g’, ‘e’, ‘e’, ‘k’, ‘s’]
The lists after insertion : [‘g’, ‘e’, ‘e’, ‘x’, ‘k’, ‘s’, ‘f’, ‘x’, ‘o’, ‘r’, ‘g’, ‘x’, ‘e’, ‘e’, ‘k’, ‘x’, ‘s’]
Method #2 : Using itertools.chain()
This method also has the ability to perform a similar task using an iterator and hence an improvement in the performance. This function performs a similar task but uses chain method to join the n substrings.
# Python3 code to demonstrate # inserting K after every Nth number # using itertool.chain() from itertools import chain # initializing list test_list = ['g', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's'] # printing original list print ("The original list is : " + str(test_list)) # initializing k k = 'x' # initializing N N = 3 # using itertool.chain() # inserting K after every Nth number res = list(chain(*[test_list[i : i+N] + [k] if len(test_list[i : i+N]) == N else test_list[i : i+N] for i in range(0, len(test_list), N)])) # printing result print ("The lists after insertion : " + str(res)) |
The original list is : [‘g’, ‘e’, ‘e’, ‘k’, ‘s’, ‘f’, ‘o’, ‘r’, ‘g’, ‘e’, ‘e’, ‘k’, ‘s’]
The lists after insertion : [‘g’, ‘e’, ‘e’, ‘x’, ‘k’, ‘s’, ‘f’, ‘x’, ‘o’, ‘r’, ‘g’, ‘x’, ‘e’, ‘e’, ‘k’, ‘x’, ‘s’]
Recommended Posts:
- Python program to insert an element into sorted list
- Python list | insert()
- Python | Insert value after each k letters in given list of string
- Python | Insert list in another list
- Python | Split given list and insert in excel file
- Python | Insert the string at the beginning of all items in a list
- List Methods in Python | Set 2 (del, remove(), sort(), insert(), pop(), extend()...)
- Python | Replace elements in second list with index of same element in first list
- Python | Indices list of matching element from other list
- Python | Slicing list from Kth element to last element
- Python | Move one list element to another list
- Python | Three element sum in list
- Python | Every Kth element in list
- Python | How to get the last element of list
- Python | Add the element in the list with help of indexing
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.



