- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
Python â€“ Append List every Nth index
When it is required to append list every ‘N’th index, a simple iteration and the ‘enumerate’ attribute are used.
Below is a demonstration of the same −
my_list = [13, 27, 48, 12, 21, 45, 28, 19, 63] print("The list is :") print(my_list) append_list = ['P', 'Y', 'T'] N = 3 print("The value of N is ") print(N) my_result =  for index, element in enumerate(my_list): if index % N == 0: for element_in in append_list: my_result.append(element_in) my_result.append(element) print("The result is :") print(my_result)
The list is : [13, 27, 48, 12, 21, 45, 28, 19, 63] The value of N is 3 The result is : ['P', 'Y', 'T', 13, 27, 48, 'P', 'Y', 'T', 12, 21, 45, 'P', 'Y', 'T', 28, 19, 63]
A list is defined and displayed on the console.
Another integer list is defined.
The value for N is defined and displayed on the console.
An empty list is created.
The list is iterated over using ‘enumerate’, and every element is divided by N and its remainder is compared to 0.
If it is 0, the element is again checked if it is present in the integer list.
If yes, it is appended to the empty list.
This is the output that is displayed on the console.
- Related Articles
- Python â€“ Append given number with every element of the list
- Python â€“ Extract Kth element of every Nth tuple in List
- Python Pandas - Append a collection of Index options together
- How to append a list to a Pandas DataFrame using append() in Python?
- How to append list to second list (concatenate lists) in Python?
- How to append objects in a list in Python?
- How to append element in the list using Python?
- Accessing nth element from Python tuples in list
- Python â€“ Index Value repetition in List
- Remove Nth Node From End of List in Python
- Consecutive Nth column Difference in Tuple List using Python
- Python Index specific cyclic iteration in list
- Equate two list index elements in Python
- Python â€“ Negative index of Element in List
- Append list of dictionaries to an existing Pandas DataFrame in Python