Indexing and Slicing

Let’s learn how to index and slice NumPy arrays.

1-D arrays (vectors)

In the simplest case, selecting one or more elements of the NumPy array works in a very similar way to Python lists. We can do the following in NumPy arrays:

  • We can grab a value using the negative index (starts with -1). We can also do the same in Python lists.

  • We can get a range from our NumPy array.

  • We can also use the negative index to grab a range from our NumPy array.

  • We can grab the values up to and from certain indexes of a NumPy array. We don’t need to give start and stop indices, since they’re optional.

Let’s create a NumPy array and grab some values from it:

Get hands-on with 1400+ tech skills courses.