# python – What does [:, :] mean on NumPy arrays

## python – What does [:, :] mean on NumPy arrays

The `[:, :]`

stands for everything from the beginning to the end just like for lists. The difference is that the first `:`

stands for first and the second `:`

for the second dimension.

```
a = numpy.zeros((3, 3))
In [132]: a
Out[132]:
array([[ 0., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.]])
```

Assigning to second row:

```
In [133]: a[1, :] = 3
In [134]: a
Out[134]:
array([[ 0., 0., 0.],
[ 3., 3., 3.],
[ 0., 0., 0.]])
```

Assigning to second column:

```
In [135]: a[:, 1] = 4
In [136]: a
Out[136]:
array([[ 0., 4., 0.],
[ 3., 4., 3.],
[ 0., 4., 0.]])
```

Assigning to all:

```
In [137]: a[:] = 10
In [138]: a
Out[138]:
array([[ 10., 10., 10.],
[ 10., 10., 10.],
[ 10., 10., 10.]])
```

numpy uses tuples as indexes.

In this case, this is a detailed slice assignment.

```
[0] #means line 0 of your matrix
[(0,0)] #means cell at 0,0 of your matrix
[0:1] #means lines 0 to 1 excluded of your matrix
[:1] #excluding the first value means all lines until line 1 excluded
[1:] #excluding the last param mean all lines starting form line 1
included
[:] #excluding both means all lines
[::2] #the addition of a second : is the sampling. (1 item every 2)
[::] #exluding it means a sampling of 1
[:,:] #simply uses a tuple (a single , represents an empty tuple) instead
of an index.
```

It is equivalent to the simpler

```
self.activity[:] = self.h
```

(which also works for regular lists as well)

#### python – What does [:, :] mean on NumPy arrays

This is slice assignment. Technically, it calls^{1}

```
self.activity.__setitem__((slice(None,None,None),slice(None,None,None)),self.h)
```

which sets all of the elements in `self.activity`

to whatever value `self.h`

is storing. The code you have there really seems redundant. As far as I can tell, you could remove the addition on the previous line, or simply use slice assignment:

```
self.activity = numpy.zeros((512,512)) + self.h
```

or

```
self.activity = numpy.zeros((512,512))
self.activity[:,:] = self.h
```

Perhaps the fastest way to do this is to allocate an empty array and `.fill`

it with the expected value:

```
self.activity = numpy.empty((512,512))
self.activity.fill(self.h)
```

^{1Actually, __setslice__ is attempted before calling __setitem__, but __setslice__ is deprecated, and shouldnt be used in modern code unless you have a really good reason for it.}