Skip to content

Python NumPy: How to Skip Every Nth Index of a NumPy Array

Learn efficient ways to remove every Nth element from a NumPy array using boolean indexing, modulo operations, and advanced techniques for data downsampling and noise filtering.

When working with large NumPy arrays, you often need to selectively exclude elements at regular intervals - for data downsampling, filtering periodic noise, or preparing datasets for analysis. Skipping every Nth element means removing items at positions 0, N, 2N, 3N, etc. (or at positions N-1, 2N-1, 3N-1, depending on your convention).

This guide covers six different approaches, from simple slicing to advanced indexing techniques, so you can choose the method that best fits your use case.

Sample Array

All examples use the following array unless otherwise noted:

import numpy as np

x = np.array([0, 1, 2, 3, 2, 5, 2, 7, 2, 9])
print("Original array:", x)

Output:

Original array: [0 1 2 3 2 5 2 7 2 9]

Method 1: Using NumPy Slicing

NumPy slicing is the most concise and efficient way to select every Nth element. The syntax array[start:stop:step] creates a view of the original array with constant time complexity:

import numpy as np

x = np.array([0, 1, 2, 3, 2, 5, 2, 7, 2, 9])

every_3rd = x[::3]
print("Every 3rd element:", every_3rd)

Output:

Every 3rd element: [0 3 2 9]

However, if you want to skip (remove) every Nth element instead of selecting it, slicing alone is not enough. You would need to combine it with other techniques.

Method 2: Using np.mod() with Boolean Indexing

This method creates evenly spaced indices using np.arange(), computes the modulo of each index against N, and keeps only the elements where the modulo is not zero:

import numpy as np

x = np.array([0, 1, 2, 3, 2, 5, 2, 7, 2, 9])
N = 3

result = x[np.mod(np.arange(x.size), N) != 0]
print("After skipping every 3rd index:", result)

Output:

After skipping every 3rd index: [1 2 2 5 7 2]

Elements at indices 0, 3, 6, and 9 (where index % 3 == 0) are excluded. This is a clean one-liner that works well for any array size.

Method 3: Using a Boolean Mask

A boolean mask gives you explicit control over which elements to keep. Create a mask of True values, then set every Nth position to False:

import numpy as np

x = np.array([0, 1, 2, 3, 2, 5, 2, 7, 2, 9])
N = 3

mask = np.ones(len(x), dtype=bool)

mask[np.arange(N - 1, len(x), N)] = False

result = x[mask]
print("After skipping every 3rd position:", result)

Output:

After skipping every 3rd position: [0 1 3 2 2 7 9]

In this example, elements at indices 2, 5, and 8 (the 3rd, 6th, and 9th positions in 1-based counting) are skipped.

Method 4: Using np.take() with np.setdiff1d()

This method calculates which indices to skip, removes them from the full index range, and uses np.take() to extract the remaining elements:

import numpy as np

x = np.array([0, 1, 2, 3, 2, 5, 2, 7, 2, 9])
N = 3

indices_to_skip = np.arange(N - 1, len(x), N)

indices_to_keep = np.setdiff1d(np.arange(len(x)), indices_to_skip)

result = np.take(x, indices_to_keep)
print("Indices skipped:", indices_to_skip)
print("Result:", result)

Output:

Indices skipped: [2 5 8]
Result: [0 1 3 2 2 7 9]

This approach is more verbose but makes the logic very explicit - useful when you need to inspect or reuse the skipped indices.

Method 5: Using np.concatenate() and np.split()

This method splits the array at every Nth index, removes the last element from each segment, and concatenates the results:

import numpy as np

x = np.array([0, 1, 2, 3, 2, 5, 2, 7, 2, 9])
N = 3

split_indices = np.arange(N, len(x), N)
segments = np.split(x, split_indices)

result = np.concatenate([s[:-1] if len(s) == N else s for s in segments])
print("Result:", result)

Output:

Result: [0 1 3 2 2 7 9]

Method 6: Naive Loop Approach

For beginners or when readability is the top priority, a simple loop with a counter works - though it sacrifices performance on large arrays:

import numpy as np

x = 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])
N = 4

result = [x[i] for i in range(len(x)) if i % N != 0]
print("After skipping every 4th index:", result)

Output:

After skipping every 4th index: [3.  6.7 8.7 1.3 4.5 6.5 3.  6.7 8.7 1.3 4.5 6.5]

Elements at indices 0, 4, 8, and 12 are removed.

Common Mistake: Confusing "Select Every Nth" with "Skip Every Nth"

A frequent source of bugs is using slicing when you intend to remove every Nth element:

import numpy as np

x = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

result = x[::3]
print("This selects, not skips:", result)

Output:

This selects, not skips: [0 3 6 9]

The result contains only the Nth elements instead of everything except them.

The correct approach to skip every 3rd element:

import numpy as np

x = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

result = x[np.mod(np.arange(len(x)), 3) != 0]
print("Correctly skipped:", result)

Output:

Correctly skipped: [1 2 4 5 7 8]

Method Comparison

Method One-Liner Performance Best For
np.mod() with boolean indexing Yes ★★★★★ General use - clean and fast
Boolean mask No ★★★★★ When you need to reuse or inspect the mask
NumPy slicing ([::N]) Yes ★★★★★ Selecting every Nth element (not skipping)
np.take() + np.setdiff1d() No ★★★★ When you need explicit index lists
np.concatenate() + np.split() No ★★★ Segmented processing workflows
Naive loop No ★★ Learning purposes or very small arrays

For most use cases, the np.mod() approach or the boolean mask method provides the best balance of readability, performance, and flexibility when skipping every Nth index in a NumPy array.

Navigation