Python: Insert a specified element in a given list after every nth element
Insert Element After Every Nth Position
Write a Python program to insert a specified element in a given list after every nth element.
Visual Presentation:

Sample Solution:
Python Code:
Sample Output:
Original list: [1, 3, 5, 7, 9, 11, 0, 2, 4, 6, 8, 10, 8, 9, 0, 4, 3, 0] Insert 20 in said list after every 4 th element: [1, 3, 5, 7, 20, 9, 11, 0, 2, 20, 4, 6, 8, 10, 20, 8, 9, 0, 4, 20, 3, 0] Original list: ['s', 'd', 'f', 'j', 's', 'a', 'j', 'd', 'f', 'd'] Insert Z in said list after every 3 th element: ['s', 'd', 'f', 'Z', 'j', 's', 'a', 'Z', 'j', 'd', 'f', 'Z', 'd']
Flowchart:

For more Practice: Solve these Related Problems:
- Write a Python program to insert a specified element after every nth element in a list, skipping positions where it already exists.
- Write a Python program to cyclically insert different elements after every nth position in a list.
- Write a Python program to insert a specified element after every nth element only if the preceding element is even.
- Write a Python program to insert a specified element after every nth position in a list and then reverse the entire list.
Python Code Editor:
Previous: Write a Python program to find common elements in a given list of lists.
Next: Write a Python program to create the largest possible number using the elements of a given list of positive integers.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.