Skip to main content

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

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])

# Select every 3rd element (indices 0, 3, 6, 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.

tip

Slicing with x[::N] selects every Nth element. To skip every Nth element (keep everything except every Nth), use the boolean mask or np.mod() approaches described below.

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

# Skip every Nth element (indices 0, 3, 6, 9 are removed)
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

# Start with all True
mask = np.ones(len(x), dtype=bool)

# Set every Nth index to False (skip indices 2, 5, 8 i.e., every 3rd position starting from index N-1)
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.

info

Note the difference in convention: The np.mod() method skips indices where index % N == 0 (indices 0, N, 2N, ...), while the boolean mask example above skips indices N-1, 2N-1, 3N-1, ... Adjust the starting index based on whether you want 0-based or 1-based Nth element skipping.

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: every 3rd position starting from index 2
indices_to_skip = np.arange(N - 1, len(x), N)

# All indices minus the skipped ones
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 at every Nth index
split_indices = np.arange(N, len(x), N)
segments = np.split(x, split_indices)

# Remove the last element of each segment (the Nth element)
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]
warning

This method is less efficient than boolean masking or np.mod() because splitting and concatenating arrays creates multiple intermediate objects. Use it only when your workflow already involves segmented processing.

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

# Keep elements whose index is NOT a multiple of N
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])

# WRONG: this SELECTS every 3rd element, not skips it
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])

# CORRECT: use boolean indexing to exclude every 3rd index
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

MethodOne-LinerPerformanceBest For
np.mod() with boolean indexingYes★★★★★General use - clean and fast
Boolean maskNo★★★★★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 loopNo★★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.