In [17]: a[a != np.array(None)]
Out[17]: array([1, 45, 23, 23, 1234, 3432, -1232, -34, 233], dtype=object)
The above works because a != np.array(None) is a boolean array which maps out non-None values:
In [20]: a != np.array(None)
Out[20]: array([ True, True, True, True, True, True, True, True, True, False], dtype=bool)
Selecting elements of an array in this manner is called boolean array indexing.
Answer from John1024 on Stack OverflowIn [17]: a[a != np.array(None)]
Out[17]: array([1, 45, 23, 23, 1234, 3432, -1232, -34, 233], dtype=object)
The above works because a != np.array(None) is a boolean array which maps out non-None values:
In [20]: a != np.array(None)
Out[20]: array([ True, True, True, True, True, True, True, True, True, False], dtype=bool)
Selecting elements of an array in this manner is called boolean array indexing.
I use the following which I find simpler than the accepted answer:
a = a[a != None]
Caveat: PEP8 warns against using the equality operator with singletons such as None. I didn't know about this when I posted this answer. That said, for numpy arrays I find this too Pythonic and pretty to not use. See discussion in comments.
The problem is that after you delete an item at position index, all subsequent indices will be shifted to the left by 1:
0 1 2 3
[a, b, c, d]
deleting at index == 1 will cause:
0 1 2
[a, c, d]
so that right after the deletion c and d have index -1 of what they had before the deletion.
The indexing is always contiguous.
So, in your code you first delete the item at position 0, all indexes gets shifted by -1 and when you advance the index, you are now considering not the second element (the one that originally was at index == 1) but the the third element (the one that originally was at index == 2), etc.
If you leave out the index += 1 line, the code should work.
Finally, please note that NumPy arrays are not particularly efficient at resizing. Python lists are generally faster at resizing.
You shouldn't delete elements from a list (or, as it happens, numpy.array) while iterating over it. Instead, create a copy:
import itertools
newArray = np.array(list(itertools.dropwhile(lambda x: x is None, numpyArray)))