In numpy, there exists some slicing operation like a[1:3,3:5], however I am confused with the operation a[:] and a[...],I am a novice at python, can anyone explain what's the difference between these?
-
`In python, there exists some slicing operation like a[1:3,3:5]` ... not in python. – cs95 Dec 26 '17 at 07:33
-
Does python support `a[...]`? – Sraw Dec 26 '17 at 07:35
-
@cᴏʟᴅsᴘᴇᴇᴅ I think this was marked as duplicate to a wrong question as I guess OP is asking slice about `numpy`. – Sraw Dec 26 '17 at 07:39
-
@Sraw No, python lists to not support indexing the the ellipsis operator (numpy arrays do). Also, the question is not tagged numpy, and I'm disinclined to reopen this... – cs95 Dec 26 '17 at 07:41
-
2There is something called [ellipsis](https://docs.python.org/dev/library/constants.html#Ellipsis) but the documentation apparently doesn't bother to say what it means. – Chris Martin Dec 26 '17 at 07:42
-
@ChrisMartin The docs are pretty clear. It is used for slicing in user-defined data types (such as numpy arrays. Try `arr[...]`). I don't think op refers to _that_ ellipsis in this question (the lack of clarity is another reason I'm not willing to reopen this). – cs95 Dec 26 '17 at 07:43
-
@COLDSPEED sorry,I should modify my question. I just read some else's code using a[...] notation, the a is a numpy's ndarray. – Schaffer Dec 26 '17 at 10:41
-
A recent non trivial use of `Ellipsis` https://stackoverflow.com/q/47919869. In indexing it means `0 or more :`. – hpaulj Dec 26 '17 at 11:19
2 Answers
The ...
is the Ellipsis, in pure Python it's basically a none-operator. It serves as a placeholder for code such as in this case:
while True:
...
Within numpy it serves a similar purpose, it's the do-not-slice operator. Since numpy supports multiple slices at the same time this can be useful. For example, to get the different edges of a cube:
In [1]: import numpy
In [2]: cube = numpy.arange(3**3).reshape(3, 3, 3)
In [3]: cube
Out[3]:
array([[[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8]],
[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]],
[[18, 19, 20],
[21, 22, 23],
[24, 25, 26]]])
In [4]: cube[0, ..., 0]
Out[4]: array([0, 3, 6])
In [5]: cube[..., 0, 0]
Out[5]: array([ 0, 9, 18])
In [6]: cube[0, 0, ...]
Out[6]: array([0, 1, 2])
It should be noted that ...
is functionally identical to :
in the cases above, but it can be different for multi-dimensional objects:
In [7]: cube[..., 0]
Out[7]:
array([[ 0, 3, 6],
[ 9, 12, 15],
[18, 21, 24]])
In [8]: cube[:, 0]
Out[8]:
array([[ 0, 1, 2],
[ 9, 10, 11],
[18, 19, 20]])
In multi-dimensional objects the ...
inserts the :
as many times as needed to reach a full dimension

- 78,177
- 11
- 137
- 148
...
means 0 or more :
. By themselves they are most useful on the LHS (but see Is there a canonical way of obtaining a 0D numpy subarray? for a RHS use)
arr[...] =1
assigns 1 to all elements of arr
. It works for all dimensions including a 0d array.
arr[:] =1
Works for a 1d array, and because trailing ':' are added automatically, higher dimensional arrays as well. It does not work on 0d arrays.
The Python object Ellipsis
https://docs.python.org/dev/library/constants.html#Ellipsis
is used by the interpreter when generating an indexing operation
a[...] # implemented as
a.__getitem__((Ellipsis,))
Similarly a ':' is converted to a slice(None)
object.
The usual Python objects like lists don't do anything with Ellipsis
, but numpy
arrays do use it.

- 221,503
- 14
- 230
- 353