Insert after every Nth element in a list Python (original) (raw)
Last Updated : 16 Jan, 2025
Inserting an element after every Nth item in a list is a useful way to adjust the structure of a list. For Example we have a list li=[1,2,3,4,5,6,7]
Let's suppose we want to insert element 'x' after every 2 elements in the list so the list will look like li=[1,2,'x',3,4,'x',5,6,7]
Iteration with index tracking
This approach iterates over the list using for loop. The loop's index keeps track of the position of the current element in the list.
Python `
a = [1, 2, 3, 4, 5, 6, 7]
The element to insert
val = 'X'
After every 2nd element
n = 2
Iterate over the list, inserting after every Nth element
res = [] # List to store the final result
for i in range(len(a)):
# Add the current element to the result list
res.append(a[i])
# Check if we've reached the Nth element
if (i + 1) % n == 0:
# Insert the desired element
res.append(val)
print(res)
`
Output
[1, 2, 'X', 3, 4, 'X', 5, 6, 'X', 7]
**Explanation:
- We loop through the original list a and for each element, append it to the res list. Loop uses the index i to determine the position of each element.
- After adding each element we check if the current index (1-based) is divisible by n (in this case, 2). we append the desired element ('X') to res after every 2nd element.
**Let's Explore other method :
Table of Content
Using a Loop
This approach goes through the list and places an element after every Nth position. It ensures the desired structure by adding the element at regular intervals.
Python `
Define the original list
a = [1, 2, 3, 4, 5, 6, 7]
Specify the interval after which the element will be inserted
n = 2 # Insert after every 2nd element
Define the element to insert into the list
val = 'X' # The element to insert
Iterate through the list and insert the element at the specified intervals
for i in range(n, len(a), n + 1): a.insert(i, val)
print(a)
`
Output
[1, 2, 'X', 3, 4, 'X', 5, 6, 7]
**Explanation: A loop iterates through the list, starting at the 2nd index (n = 2
) and inserting 'X'
after every 2nd position using the insert() method.
Using extend() Method
This method builds a new list using the extend() function, adding the desired element at regular intervals. It’s a simple way to create a structured list
Python `
a = [1, 2, 3, 4, 5, 6, 7] n = 2 # Insert after every 2nd element element = 'X'
Extend the list by inserting after every Nth element
b = [] for i in range(len(a)): b.append(a[i]) if (i + 1) % (n + 1) == 0: b.append(element)
print(b)
`
Output
[1, 2, 3, 'X', 4, 5, 6, 'X', 7]
**Explanation: The code iterates through the list a, appending each element to the new list b. It checks if the current index (i + 1) is divisible by n + 1 to insert the element ('X') after every n-th element.
itertools.chain() function can combine the original items with the inserted elements to create a new list. It’s a easy way to merge everything seamlessly.
Python `
import itertools
a = [1, 2, 3, 4, 5, 6, 7] n = 2 # Insert after every 2nd element element = 'X'
Using itertools.chain to insert elements
b = list(itertools.chain(*[(a[i], element) if (i + 1) % (n + 1) == 0 else (a[i],) for i in range(len(a))]))
print(b)
`
Output
[1, 2, 3, 'X', 4, 5, 6, 'X', 7]
**Explanation:
- Code uses itertools.chain() to merge pairs of the original list elements and the inserted element 'X'. It iterates through the list, adding 'X' after every Nth element, based on the condition (i + 1) % (n + 1) == 0.
- Final list is constructed by unpacking the tuples created during the iteration and combining them into a new list, which is then printed.