Skip to content
Home » numpy Part 6 – Slicing numpy Arrays

numpy Part 6 – Slicing numpy Arrays

Spread the love

Today we’ll be talking about slicing numpy arrays.

Slicing 1-Dimensional Arrays

As far as slicing is concerned, there’s nothing special about it as long as we’re dealing with 1-dimensional arrays. It works just like with any regular Python sequences.

Here’s how slicing works with a list:

>>> lst = [5, 15, 25, 35, 45, 55, 65, 75]
>>> lst[2:6]
[25, 35, 45, 55]
>>> lst[4:]
[45, 55, 65, 75]
>>> lst[:5]
[5, 15, 25, 35, 45]

And now let’s create a numpy array:

>>> arr = np.array(lst)
>>> print(arr)
[ 5 15 25 35 45 55 65 75]

This is what you get if you echo the slice in interactive mode:

>>> arr[2:6]
array([25, 35, 45, 55])

And this is what you get if you print it:

>>> print(arr[2:6])
[25 35 45 55]
>>> print(arr[4:])
[45 55 65 75]
>>> print(arr[:5])
[ 5 15 25 35 45]

Slicing Multidimensional Arrays

And now let’s have a look at multidimensional arrays. How do you slice them?

You separate the ranges for each dimension by commas inside one pair of square brackets.

Here’s a 2-dimensional array:

>>> a = np.array([
...     [10, 20, 30, 40, 50],
...     [11, 21, 31, 41, 51],
...     [12, 22, 32, 42, 52],
...     [13, 23, 33, 43, 53],
...     [14, 24, 34, 44, 54],
...     [15, 25, 35, 45, 55]])
>>> print(a)
[[10 20 30 40 50]
 [11 21 31 41 51]
 [12 22 32 42 52]
 [13 23 33 43 53]
 [14 24 34 44 54]
 [15 25 35 45 55]]

Now let’s make some slices. To better visualize what’s going on, I’m going to add some images so that you can see which elements are in the slice.

Here’s the original array:

original array

Your Panda3D Magazine

Make Awesome Games and Other 3D Apps

with Panda3D and Blender using Python.

Cool stuff, easy to follow articles.

Get the magazine here (PDF).

Let’s slice the array. We want the rows from 1 to 3 and the columns from 2 to the end:

>>> print(a[1:4, 2:])
[[31 41 51]
 [32 42 52]
 [33 43 53]]
slice array

And now we want the rows from the beginning to 2 and the columns from 1 to 2:

>>> print(a[:3, 1:3])
[[20 30]
 [21 31]
 [22 32]]
slice array

Now we’ll make a slice with all rows and columns:

>>> print(a[:, :])
[[10 20 30 40 50]
 [11 21 31 41 51]
 [12 22 32 42 52]
 [13 23 33 43 53]
 [14 24 34 44 54]
 [15 25 35 45 55]]
slice array

If we skip the indices for some dimensions, they are automatically set to :, so the whole dimension gets into the slice. Here’s an example. We want the rows from 1 to 2 and all the columns:

>>> print(a[1:3])
[[11 21 31 41 51]
 [12 22 32 42 52]]
slice array

Python Jumpstart Course

Learn the basics of Python, including OOP.

with lots of exercises, easy to follow

The course is available on Udemy.

The Step

You can also use the step. Here we want every other row and the columns from the beginning to 2:

>>> print(a[::2, :3])
[[10 20 30]
 [12 22 32]
 [14 24 34]]
slice array

And one more example with the step. This time we want every other row and every other column:

>>> print(a[::2, ::2])
[[10 30 50]
 [12 32 52]
 [14 34 54]]

There’s one more thing about slicing numpy arrays. There’s a difference between how list slices and array slices behave. Have a look:

First let’s define a list and make an array out of it:

>>> a = [1, 2, 3, 4, 5, 6]
>>> A = np.array(a)
>>> print(A)
[1 2 3 4 5 6]

Now let’s slice the list and the array:

>>> list_slice = a[1:5]
>>> array_slice = A[1:5]
>>> print(list_slice)
[2, 3, 4, 5]
>>> print(array_slice)
[2 3 4 5]

Blender Jumpstart Course

Learn the basics of 3D modeling in Blender.

step-by-step, easy to follow, visually rich

The course is available on Udemy and on Skillshare.

Now let’s change the first element in each slice to 10:

>>> list_slice[0] = 10
>>> array_slice[0] = 10

If we now print the list, we get the original list without the change we made in the slice:

>>> print(a)
[1, 2, 3, 4, 5, 6]

But look what happens if we print the array. The change we made in the slice is also visible in the array:

>>> print(A)
[ 1 10  3  4  5  6]

Now, why is that? Well, when we slice a list, we create a new object and when we slice an array, we don’t create a new object. Instead, we create a view on the original array and any change we make on the view will also modify the array.

Here’s the video version:


Spread the love

Leave a Reply