How to Skip every Nth index of Numpy array?


In Python, we have some built-in functions like array(), len(), append, and, mod() that can be use to Skip every Nth index of Numpy array. There are many ways for skipping the nth index of a NumPy array. The numpy modulus technique is one option. We can first arrange the array into chunks of evenly spaced intervals using the numpy.arange() method. Then, using the nth index, apply the np.mod() technique to the generated list intervals and compute the modulus of each element.

Syntax

The following syntax is used in the examples −

array()

This is an in-built method that takes the list as input.

mod(first_item, Second_item)

The mod returns the remaining number by dividing the first item by the second item.

arange()

This is the built-in method in Python that can be used to set the numerical range of an array.

len()

This is an in-built method in Python that can be used to find the length of the object.

append()

The append method accepts one parameter that adds the integer at the end of the given list.

Example 1

In the following example, begin the program by importing the module named numpy and take the reference object as np. Then use the np.mod() method that interval the list over the method and compute each mod element with Nth index. The elements of the original array whose mod output is not 0 are returned the result as the final list.

import numpy as np
x = np.array([10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140])
new_arr = x[np.mod(np.arange(x.size), 5) != 0]
print("After Skipping the Nth index:")
print(new_arr)

Output

After Skipping the Nth index:
[ 20  30  40  50  70  80  90 100 120 130 140]

Example 2

In the following example, start the program by mentioning the module named numpy and take the reference as np. Then use the array method that converts all the number into a list and store it in the variable x. Then find the length of x using the len method. Now use the slicing technique that removes the number existing between the 0th to 4th index position and repeat the same process till the end of the list. This process will store in the variable current_arr. Finally, we are printing the result with the help of variable current_arr.

# Numpy Slicing
import numpy as np
x = np.array([9, 11, 12, 33, 32, 55, 88, 97, 23, 19, 86, 11, 3])
length = len(x)
current_arr = x[0:length:4]
print("List after n=4th element access:")
print(current_arr)

Output

List after n=4th element access:
[ 9 32 23  3]

Example 3

In the following example, we will skip the even index position of the number in an array. Begin the program by importing the module named numpy and take the object reference as np. Then use the array() method that converts the integer into the list and stores it in the variable num. Then set the Nth index i.e. n=2 that will remove even index position. Next, use the empty list in the variable emp_arr that stores the rest integer after removing the even index position integer. Moving ahead to use the for loop where integer i iterate into the original array i.e. num and then use the if-statement where all the elements are appended to a new list without the Nth index element encounter while traversing on each index position. All these processes will be stored in the variable emp_arr. Finally, we are printing the result with the help of the variable named emp_arr.

import numpy as np
num = np.array([124, 301, 627, 387, 812, 113, 145, 65])
n = 2 # Nth index to set the removal of an even position
# store the present number after skipping the Nth position
emp_arr = [] 
cnt = 0
for i in num:
   if cnt % n != 0:
      emp_arr.append(i)
   cnt += 1
print("Array after skipping nth element:\n",emp_arr)

Output

 Array after skipping nth element:
 [301, 387, 113, 65]

Conclusion

We discussed the different ways to skip the Nth index position in a given array of the program. The most important part is NumPy module which is also known as the powerful library for complex mathematical operations and has a wide range of applications in various fields such as Data Science, Data Analysis, and Machine Learning. It overcomes slower executions with the use of multi-dimensional array objects and has built-in functions for manipulating arrays like append(), arange(), len(), etc.

Updated on: 17-Jul-2023

515 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements