Today, we’re going to explain how to access NumPy array items via indexing. After that, we’ll show you how to slice a NumPy array to select a range, or a subset, of values from our arrays. We’re going to run our indexing and slicing examples on 1-Dimensional, 2-Dimensional and 3-Dimensional NumPy arrays. This will give you everything you need to know to index and slice on any N-dimensional NumPy array and it’s especially useful when working with sorted NumPy arrays.

## Indexing NumPy arrays

Indexing refers to accessing items from an array using index numbers. Like regular Python lists, NumPy arrays follow a zero-based indexing scheme where the first item exists at the 0th index and the last item exists at N-1 index, where N is the total number of items in a NumPy array.

### Indexing 1-D NumPy Arrays

Indexing a 1-dimensional NumPy array is simple. You just have to pass the one index value inside the square brackets following the array name, just like you would in a Python list.

Let’s walk through an example. The script below defines a NumPy array with 10 items.

``````import numpy as np

ints = np.array([1,2,3,4,5,6,7,8,9,10])
print(ints)``````

Output:

``[ 1  2  3  4  5  6  7  8  9 10]``

The following script prints the value of the item at the 0th index of the ints array we just defined.

``print(ints[0])``

Output:

``1``

Let’s see another example. This time, we’ll return the item at the 7th index of the ints array. The 7th index contains the 8th item, so you’ll see 8 in the output of the script below:

``print(ints[7])``

Output:

``8``

You can also access array items via negative indexing. For instance, the following script returns the 4th item from the end of our ints NumPy array.

``print(ints[-4])``

Output:

``7``

### Indexing 2-D NumPy Arrays

To index items in a 2-D NumPy array, you have to pass two comma-separated index values corresponding to your 2 dimensions. The script below creates a 2-D NumPy array named int_2d. The array contains 4 rows and 3 columns.

``````import numpy as np
int_2d = np.array([[ 7, 4, 11],
[ 5, 11, 16],
[11,  9, 18],
[16,  5, 18]]
)
print(int_2d)``````

Output:

``````[[ 7  4 11]
[ 5 11 16]
[11  9 18]
[16  5 18]]``````

To access items from the int_2d array, the first index value will correspond to the row number and the second index value corresponds to the column number. For instance, in the following script, values [2,1] are passed as index values. In the output, the item from the 3rd row (row at 2nd index is actually the third row), and the 2nd column (again column at index 1 is actually the second column) will be returned.

``print(int_2d[2,1])``

Output:

``9``

Similarly, the script below will return the item from the 4th row and 3rd column of the int_2d array i.e. 18.

``print(int_2d[3,2])``

Output:

``18``

Just like with 1-D arrays, you can use negative indexing to return items from a 2-D array. For instance, the script below returns the item at the 3rd row from the end, and 1st column from the end of our int_2d array, which is 16.

``print(int_2d[-3,-1])``

Output:

``16``

You can combine negative indexing and positive indexing if you’d like, as well.

### Indexing 3-D NumPy Arrays

A 3-D NumPy array is a collection of multiple 2-D arrays. To access items from a 3-D NumPy array, you need to pass three index values separated by commas. The first index value corresponds to the index of the 2-D array that you want to select, the second index value correspond to the row number of the 2-D array selected by the first index value, and the third index value corresponds to the column number of the 2-D array selected by the first index value. Let’s see an example.

The following script defines a 3-D NumPy array int_3d with three items (three 2-D arrays).

``````import numpy as np
int_3d = np.array([
[[ 7, 4, 11],
[ 5, 11, 16],
[11,  9, 18]],

[[ 5, 5, 15],
[ 9, 6, 10],
[15, 9, 13]],

[[9, 7, 12],
[24, 8, 9],
[8, 21, 5]]
]
)
print(int_3d)``````

Output:

``````[[[ 7  4 11]
[ 5 11 16]
[11  9 18]]

[[ 5  5 15]
[ 9  6 10]
[15  9 13]]

[[ 9  7 12]
[24  8  9]
[ 8 21  5]]]``````

The following script selects an item from the 2nd 2-D array, 3rd row, and 3rd column i.e. 13.

``print(int_3d[1,2,2])``

Output:

``13``

Let’s see another example. The script below returns the item from the 1st row and 2nd column of the 2-D array at the 2nd index within our 3-D array.

``print(int_3d[2,0,1])``

Output:

``7``

As you probably guessed, you can use negative indexing to access items from a 3-D NumPy array, as well. For example, the script below returns items from the 2nd 2-D array from the end, 3rd row from the end, and 1st column from the end.

``print(int_3d[-2,-3,-1])``

Output:

``15``

I put together a Python Developer Kit with over 100 pre-built Python scripts covering data structures, Pandas, NumPy, Seaborn, machine learning, file processing, web scraping and a whole lot more - and I want you to have it for free. Enter your email address below and I'll send a copy your way.

Yes, I'll take a free Python Developer Kit

## Slicing NumPy Arrays

When we say we’re slicing a NumPy array, what we mean is we’re selecting a subset of items from our NumPy array using a range of index values.

### Slicing 1-D NumPy Arrays

To slice a 1-D NumPy array, you need to pass two values: the start index and end index in square brackets following the NumPy array name. The index values should be separated by a colon. In the output, you’ll see a new NumPy array with all the items from the starting index to 1 less than the end index.

Let’s see an example. The script below defines a NumPy array with 10 items.

``````import numpy as np

ints = np.array([1,2,3,4,5,6,7,8,9,10])``````

To select items from the 2nd to 8th index from this ints array, you need to pass [2:9] as the range of indices as shown below:

``````ints_sliced = ints[2:9]
print(ints_sliced)``````

Output:

``[3 4 5 6 7 8 9]``

If you don’t pass any value for the start index, it defaults to 0. For instance, the following script returns all the items from the 0th index to 4th index.

``````ints_sliced = ints[:5]
print(ints_sliced)``````

Output:

``[1 2 3 4 5]``

Similarly, if you don’t pass any value for the ending index, it will pull everything to the end of your array. For example, the following script returns items from the 3rd index to the end of the ints NumPy array.

``````ints_sliced = ints[3:]
print(ints_sliced)``````

Output:

``[ 4  5  6  7  8  9 10]``

As you saw with indexing, you can also use negative values for slicing. For instance, the script below returns the last two values from the ints array.

``````ints_sliced = ints[-2:]
print(ints_sliced)``````

Output:

``[ 9 10]``

Similarly, the following script returns all the items from 0th index to the end of the NumPy array, excluding the last 4 items.

``````ints_sliced = ints[:-4]
print(ints_sliced)``````

Output:

``[1 2 3 4 5 6]``

### Slicing 2-D NumPy Arrays

For slicing 2-D NumPy arrays, you need to pass two ranges of values: one range for rows and the other range for the columns.

The following script defines a sample 2-D NumPy array that we’ll in our examples in this section.

``````import numpy as np
int_2d = np.array([[ 7, 4, 11],
[ 5, 11, 16],
[11,  9, 18],
[16,  5, 18]]
)
print(int_2d)``````

Output:

``````[[ 7  4 11]
[ 5 11 16]
[11  9 18]
[16  5 18]]``````

The script below returns all the items from 2nd to 4th rows, and 2nd to 3rd columns.

``````ints_sliced = int_2d[1:4,1:3]
print(ints_sliced)``````

Output:

``````[[11 16]
[ 9 18]
[ 5 18]]``````

Similarly, the script below returns values from all the rows of 2nd and 3rd columns. Notice how we took advantage of the default first and last index position by simply specifying `:` as our first argument in the slicing pair.

``````ints_sliced = int_2d[:,1:3]
print(ints_sliced)``````

Output:

``````[[ 4 11]
[11 16]
[ 9 18]
[ 5 18]]``````

Just like with every example we’ve seen so far, you can use negative indexing to slice 2-D arrays. As an example, the following script selects all the items from the last two rows and last two columns of our int_2d array.

``````ints_sliced = int_2d[-2:,-2:]
print(ints_sliced)``````

Output:

``````[[ 9 18]
[ 5 18]]``````

### Slicing 3-D NumPy Arrays

Finally, slicing can also be applied on 3-D arrays. Let’s define a simple 3-D array.

``````import numpy as np
int_3d = np.array([
[[ 7, 4, 11],
[ 5, 11, 16],
[11,  9, 18]],

[[ 5, 5, 15],
[ 9, 6, 10],
[15, 9, 13]],

[[9, 7, 12],
[24, 8, 9],
[8, 21, 5]]
]
)
print(int_3d)``````

Output:

``````[[[ 7  4 11]
[ 5 11 16]
[11  9 18]]

[[ 5  5 15]
[ 9  6 10]
[15  9 13]]

[[ 9  7 12]
[24  8  9]
[ 8 21  5]]]``````

The following script grabs items from the last two rows and first two columns of the first two items of the int_3d array.

``````ints_sliced = int_3d[0:2, -2:, 0:2]
print(ints_sliced)``````

Output:

``````[[[ 5 11]
[11  9]]

[[ 9  6]
[15  9]]]``````

This is pretty cool, isn’t it? You can imagine how useful indexing and slicing would be, especially when working with sorted NumPy arrays. If you want more tips for working with NumPy arrays, subscribe using the form below.