You can use numpy's slicing, simply start:stop:step.

>>> xs
array([1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4])
>>> xs[1::4]
array([2, 2, 2])

This creates a view of the the original data, so it's constant time. It'll also reflect changes to the original array and keep the whole original array in memory:

>>> a
array([1, 2, 3, 4, 5])
>>> b = a[::2]         # O(1), constant time
>>> b[:] = 0           # modifying the view changes original array
>>> a                  # original array is modified
array([0, 2, 0, 4, 0])

so if either of the above things are a problem, you can make a copy explicitly:

>>> a
array([1, 2, 3, 4, 5])
>>> b = a[::2].copy()  # explicit copy, O(n)
>>> b[:] = 0           # modifying the copy
>>> a                  # original is intact
array([1, 2, 3, 4, 5])

This isn't constant time, but the result isn't tied to the original array. The copy also contiguous in memory, which can make some operations on it faster.

Answer from behzad.nouri on Stack Overflow
🌐
Finxter
blog.finxter.com › 5-best-ways-to-extract-every-nth-element-from-a-numpy-array
5 Best Ways to Extract Every Nth Element from a NumPy Array – Be on the Right Side of Change
The simplest method to extract every nth element from a NumPy array is through array slicing syntax arr[start::n], where start is the starting index (usually 0 for the first element) and n is the step size.
🌐
Data Science Parichay
datascienceparichay.com › home › blog › numpy – get every nth element in array
Numpy - Get Every Nth Element in Array - Data Science Parichay
August 11, 2022 - You can use slicing to get every nth element of a Numpy array. Slice the array from its start to end and use n as the step parameter.
Discussions

When wanting to select every Nth element while iterating through an array, is it more 'pythonic' to use slice notation or the modulo operator?
I'd go with this: for x in range(0, 100, 20): More on reddit.com
🌐 r/learnpython
2
2
March 9, 2021
python - Sample every nth element of numpy array, repeat by moving start index m times - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
🌐 stackoverflow.com
python - Skip every nth index of numpy array - Stack Overflow
In order to do K-fold validation I would like to use slice a numpy array such that a view of the original array is made but with every nth element removed. For example: [0, 1, 2, 3, 4, 5, 6, 7, 8, ... More on stackoverflow.com
🌐 stackoverflow.com
December 2, 2016
python - Copy every nth column of a numpy array - Stack Overflow
0 Efficient Numpy multiple sampling which results in a Matrix · 3 Construct array by sampling over every n'th element along last axis More on stackoverflow.com
🌐 stackoverflow.com
People also ask

What is subsampling in numpy?
A: Subsampling in numpy refers to selecting a subset of elements from a numpy array at regular intervals (e.g., every nth element). This operation is useful when working with large data sets or when reducing the size of an array for analysis.
🌐
sqlpey.com
sqlpey.com › python › subsample-every-nth-entry-in-a-numpy-array
Solved: How to Subsample Every nth Entry in a Numpy Array - …
Can I subsample an array using other Python libraries?
A: Yes, other Python libraries like Pandas can also be used for subsampling operations. However, NumPy is generally the most efficient for numerical data manipulation.
🌐
sqlpey.com
sqlpey.com › python › subsample-every-nth-entry-in-a-numpy-array
Solved: How to Subsample Every nth Entry in a Numpy Array - …
What is the most efficient way to subsample data from an array?
A: The most efficient way depends on the use case. For simple interval-based subsampling, using array slicing (a[start::step]) is the quickest. For more complex needs, using numpy.linspace can be helpful.
🌐
sqlpey.com
sqlpey.com › python › subsample-every-nth-entry-in-a-numpy-array
Solved: How to Subsample Every nth Entry in a Numpy Array - …
Top answer
1 of 2
259

You can use numpy's slicing, simply start:stop:step.

>>> xs
array([1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4])
>>> xs[1::4]
array([2, 2, 2])

This creates a view of the the original data, so it's constant time. It'll also reflect changes to the original array and keep the whole original array in memory:

>>> a
array([1, 2, 3, 4, 5])
>>> b = a[::2]         # O(1), constant time
>>> b[:] = 0           # modifying the view changes original array
>>> a                  # original array is modified
array([0, 2, 0, 4, 0])

so if either of the above things are a problem, you can make a copy explicitly:

>>> a
array([1, 2, 3, 4, 5])
>>> b = a[::2].copy()  # explicit copy, O(n)
>>> b[:] = 0           # modifying the copy
>>> a                  # original is intact
array([1, 2, 3, 4, 5])

This isn't constant time, but the result isn't tied to the original array. The copy also contiguous in memory, which can make some operations on it faster.

2 of 2
2

Complementary to behzad.nouri's answer: If you want to control the number of final elements and ensure it's always fixed to a predefined value (rather than controlling a fixed step in between subsamples) you can use numpy's linspace method followed by integer rounding.

For example, with num_elements=4:

>>> a
array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
>>> choice = np.round(np.linspace(1, len(a)-1, num=4)).astype(int)
>>> a[choice]
array([ 2,  5,  7, 10])

Or, subsampling an array with final start/end points in general:

>>> import numpy as np
>>> np.round(np.linspace(0, len(a)-1, num=4)).astype(int)
array([0, 3, 6, 9])
>>> np.round(np.linspace(0, len(a)-1, num=15)).astype(int)
array([0, 1, 1, 2, 3, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9])
🌐
ProjectPro
projectpro.io › recipes › select-elements-from-numpy-array-in-python
How to Select Columns in NumPy Array using np.select? -
February 22, 2024 - ... The expression arr[:, 1:3] selects all rows (indicated by :) and the second and third columns (columns with index 1 and 2). Adjust the column indices in the slice as needed. You can use array slicing with a step size to select every nth ...
🌐
IncludeHelp
includehelp.com › python › subsampling-every-nth-entry-in-a-numpy-array.aspx
Python - Subsampling every nth entry in a NumPy array
There is a common approach of slicing and repeating the elements after some steps. Start:Stop:Step fashion allows us to solve this problem. ... # Import numpy import numpy as np # Creating a numpy array arr = np.array( [1,2,3,4,5,6,7,8,9,10, 11,12,13,14,15,16,17,18,19,20] ) # Display original array print("Original array:\n",arr,"\n") # Slicing every 5th element and starting # from 1st element res = arr[0::5] # Display result print("Subsampled array:\n",res,"\n")
🌐
Reddit
reddit.com › r/learnpython › when wanting to select every nth element while iterating through an array, is it more 'pythonic' to use slice notation or the modulo operator?
r/learnpython on Reddit: When wanting to select every Nth element while iterating through an array, is it more 'pythonic' to use slice notation or the modulo operator?
March 9, 2021 -

For example, if I want to iterate through an array and select only every 20th element, is it considered more 'pythonic' to do it like this...

for x in range(100)[::20]:
    pass

...or this...

for x in range(100):
    if x % 20 == 0:
        pass

I read through PEP 204 (rejected) but it only covered list ranges and slice notation for stepping, not modulo operators.

Thanks!

🌐
Iditect
iditect.com › faq › python › subsampling-every-nth-entry-in-a-numpy-array.html
Subsampling every nth entry in a numpy array
import numpy as np # Create a sample NumPy array original_array = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) # Define the step size step = 3 # Subsample every nth entry subsampled_array = original_array[::step] print(subsampled_array) In this example, the original_array is subsampled by selecting every 3rd element using the slicing notation [::step]. The result is [0, 3, 6, 9].
Find elsewhere
🌐
sqlpey
sqlpey.com › python › subsample-every-nth-entry-in-a-numpy-array
Solved: How to Subsample Every nth Entry in a Numpy Array - …
December 5, 2024 - A: Subsampling in numpy refers to selecting a subset of elements from a numpy array at regular intervals (e.g., every nth element).
🌐
W3Schools
w3schools.com › python › numpy › numpy_array_slicing.asp
NumPy Array Slicing
Slice elements from the beginning to index 4 (not included): import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7]) print(arr[:4]) Try it Yourself » · Use the minus operator to refer to an index from the end: Slice from the index 3 from the end to index 1 from the end: import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7]) print(arr[-3:-1]) Try it Yourself » · Use the step value to determine the step of the slicing: Return every other element from index 1 to index 5:
🌐
TutorialsPoint
tutorialspoint.com › how-to-skip-every-nth-index-of-numpy-array
How to Skip every Nth index of Numpy array?
Original array: [ 10 20 30 40 50 ... provides a simpler way to access every Nth element directly using the step parameter in slice notation [start:stop:step]....
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-skip-every-nth-index-of-numpy-array
How to skip every Nth index of NumPy array ? - GeeksforGeeks
July 23, 2025 - 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. ... # importing required packages import numpy as np # declaring a numpy array 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]) # skipping every 4th element n = 4 # declaring new list new_arr = [] cntr = 0 # looping over array for i in x: if(cntr % n != 0): new_arr.append(i) # incrementing counter cntr += 1 print("Array after skipping nth element") print(new_arr)
🌐
Stack Overflow
stackoverflow.com › questions › 73097727 › sample-every-nth-element-of-numpy-array-repeat-by-moving-start-index-m-times
python - Sample every nth element of numpy array, repeat by moving start index m times - Stack Overflow
Title might be confusing, so it's easier if I explain what my goal is with some code: arr = np.array([1,1,1,2,2,2,3,3,3,4,4,4]) n_sub = 3 outs = [arr[i::n_sub] for i in range(n_sub)] # outs returns [
Top answer
1 of 3
13

Approach #1 with modulus

a[np.mod(np.arange(a.size),4)!=0]

Sample run -

In [255]: a
Out[255]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [256]: a[np.mod(np.arange(a.size),4)!=0]
Out[256]: array([1, 2, 3, 5, 6, 7, 9])

Approach #2 with masking : Requirement as a view

Considering the views requirement, if the idea is to save on memory, we could store the equivalent boolean array that would occupy 8 times less memory on Linux system. Thus, such a mask based approach would be like so -

# Create mask
mask = np.ones(a.size, dtype=bool)
mask[::4] = 0

Here's the memory requirement stat -

In [311]: mask.itemsize
Out[311]: 1

In [312]: a.itemsize
Out[312]: 8

Then, we could use boolean-indexing as a view -

In [313]: a
Out[313]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [314]: a[mask] = 10

In [315]: a
Out[315]: array([ 0, 10, 10, 10,  4, 10, 10, 10,  8, 10])

Approach #3 with NumPy array strides : Requirement as a view

You can use np.lib.stride_tricks.as_strided to create such a view given the length of the input array is a multiple of n. If it's not a multiple, it would still work, but won't be a safe practice, as we would be going beyond the memory allocated for input array. Please note that the view thus created would be 2D.

Thus, an implementaion to get such a view would be -

def skipped_view(a, n):
    s = a.strides[0]
    strided = np.lib.stride_tricks.as_strided
    return strided(a,shape=((a.size+n-1)//n,n),strides=(n*s,s))[:,1:]

Sample run -

In [50]: a = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]) # Input array

In [51]: a_out = skipped_view(a, 4)

In [52]: a_out
Out[52]: 
array([[ 1,  2,  3],
       [ 5,  6,  7],
       [ 9, 10, 11]])

In [53]: a_out[:] = 100 # Let's prove output is a view indeed

In [54]: a
Out[54]: array([  0, 100, 100, 100,   4, 100, 100, 100,   8, 100, 100, 100])
2 of 3
2

numpy.delete :

In [18]: arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [19]: arr = np.delete(arr, np.arange(0, arr.size, 4))

In [20]: arr
Out[20]: array([1, 2, 3, 5, 6, 7, 9])
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.take.html
numpy.take — NumPy v2.5 Manual
‘clip’ mode means that all indices that are too large are replaced by the index that addresses the last element along that axis.
🌐
Reddit
reddit.com › r/learnpython › i need help with summing every n elements in a list.
r/learnpython on Reddit: I need help with summing every N elements in a list.
February 12, 2016 -

Hi! So I have some data with millisecond resolution but I am really only concerned with looking at it on a second-by-second basis. Essentially I want to sum every thousand elements in my list in order to rebin the data to seconds, I am pretty stuck trying to think of a way to do this, if anyone has a solution I'd be really grateful.

Edit: I am pretty sure I solved this with the following:

b=[]
for i in range(0, len(counts), 999):
    b.append(sum(counts[i:i+999])

The list is pretty big so it's still running, if anyone sees a reason this wouldn't work please let me know.

🌐
GeeksforGeeks
geeksforgeeks.org › python-multiply-k-to-every-nth-element
Python - Multiply K to every Nth element - GeeksforGeeks
May 16, 2023 - The original list is : [1, 4, 5, ... list, N and K values. Convert the original list to numpy array. Use numpy's indexing feature to select the Nth ......
🌐
Earth Data Science
earthdatascience.org › home
Slice (or Select) Data From Numpy Arrays | Earth Data Science - Earth Lab
September 23, 2019 - Just like for the one-dimensional numpy array, you use the index [1,2] for the second row, third column because Python indexing begins with [0], not with [1] On this page, you will use indexing to select elements within one-dimensional and two-dimensional numpy arrays, a selection process referred to as slicing.