Colon, None, slice(None) in numpy array indexers
Using a raw None
(not in slice
) is the same thing as using np.newaxis
, of which it is but an alias.
In your case:
a[0,None,1]
is like a[0,np.newaxis,1]
, hence the output- whereas
slice(None)
is like "slice nothing", which is whya[0,:,1]
is the same asa[0,slice(None),1]
. See numpy's Indexing doc.
a[0,None,1]
is the same as a[0, 1]
but with an extra axis in the result.
The
newaxis
object can be used in all slicing operations to create an axis of length one.:const: newaxis
is an alias for‘None’
, and‘None’
can be used in place of this with the same result.
So a[0,None,1]
is the same as a[0,np.newaxis,1]
In this case, where None
is placed is not of relevance, but every None
adds a new axis.
>>> a[0,None, 1]array([[4, 5, 6, 7]])>>> a[None,None,0,1]array([[[4, 5, 6, 7]]])>>> a[0,np.newaxis,1]array([[4, 5, 6, 7]])