Social Media

Python – remove nth element from array

I was looking at sampling algorithms, and realised it was overkill for my task. Instead I found a useful numpy feature –

import numpy as np

my_array = np.array([1,2, 3, 4, 5, 6, 7, 8, 9, 10,11,12])
my_array = np.delete(x, np.arange(0, x.size, 3))

Output - 
[2 3 5 6 8 9 11 12]

Here we remove every 3rd element! Easy!

About the Author Martin Farrell

My name is Martin Farrell. I have almost 20 years Java experience. I specialize inthe Spring Framework and JEE. I’ve consulted to a range of businesses, and have provide Java and Spring mentoring and training. You can learn more at About or on my consultancy website Glendevon Software

follow me on:

Leave a Comment: