How to skip every Nth index of NumPy array ? - GeeksforGeeks

21 Apr, 2021
In this article, we will see how to skip every Nth index of the NumPy array. There are various ways to access and skip elements of a NumPy array : 
Method 1: Naive Approach
A counter can be maintained to keep a count of the elements traversed so far, and then as soon as the Nth position is encountered, the element is skipped and the counter is reset to 0. All the elements are appended to a new list excluding the Nth index element encountered while traversal. The time required during this is equivalent to O(n), where n is the size of the numpy array. In case the elements need to be just printed and not stored, we can skip the declaration of creation of another array.
Example:
Python3
# importing required packagesimport numpy as np  # declaring a numpy arrayx = np.array([1.2, 3.0, 6.7, 8.7, 8.2,              1.3, 4.5, 6.5, 1.2, 3.0,              6.7, 8.7, 8.2, 1.3, 4.5,               6.5])  # skipping every 4th elementn = 4  # declaring new listnew_arr = []  # maintaining cntrcntr = 0  # looping over arrayfor i in x:        # checking if element is nth pos    if(cntr % n != 0):        new_arr.append(i)              # incrementing counter    cntr += 1  print("Array after skipping nth element")print(new_arr)
Output:
Array after skipping nth element
[3.0, 6.7, 8.7, 1.3, 4.5, 6.5, 3.0, 6.7, 8.7, 1.3, 4.5, 6.5]
Method 2: Using NumPy modulus method
The array can first be arranged into chunks of evenly spaced intervals, using the numpy.arange() method.
Syntax: np.arange(start,stop,step)
Parameter:
start: Start of the interval
stop: End of the interval
step: Steps between the start and end interval
Then, the np.mod() method is applied over the list’s intervals obtained and each element’s modulo is then computed with the nth index. The elements of the original array whose modulo output is not 0, are returned as the final list. 
Example
Python3
# importing required packagesimport numpy as np  # declaring a numpy arrayx = np.array([0, 1, 2, 3, 2, 5, 2, 7, 2, 9])  print("Original Array")print(x)  # skipping third elementnew_arr = x[np.mod(np.arange(x.size), 3) != 0]  print("Array after skipping elements : ")print(new_arr)
Output:
Original Array
[0 1 2 3 2 5 2 7 2 9]
Array after skipping elements :  
[1 2 2 5 7 2]
Method 3: NumPy Slicing 
NumPy slicing is basically data subsampling where we create a view of the original data, which incurs constant time. The changes are made to the original array and the entire original array is kept in memory. A copy of the data can also be made explicitly. 
Syntax:
arr[start:end:st]
Here, where the start is the starting index, the end is the stopping index, and st is the step, where the step is not equivalent to 0. And, it returns a sub-array that contains the elements belonging to the st index respectively. The indexes of the array are assumed to be starting at 0. 
Example
Python
# importing required packagesimport numpy as np  # declaring a numpy arrayx = np.array([0, 1, 2, 3, 2, 5, 2, 7, 2, 9])  # calculating length of arraylength = len(x)  # accessing every third element # from the arrayprint("List after n=3rd element access")print(x[0:length:3])
Output:
List after n=3rd element access
[0 3 2 9] 
Picked
Python numpy-Indexing
Python-numpy
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Install PIP on Windows ?
How to drop one or multiple columns in Pandas Dataframe
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Python | Pandas dataframe.groupby()
Defaultdict in Python
Python | Get unique values from a list
Python Classes and Objects
Python | os.path.join() method
Create a directory in Python