NumPy Basics - Array Indexing and Slicing

3. Array Indexing: Accessing Single Elements

If we want to get and set the values of individual elements in the array, we need to be able to access single elements, correct? Accessing single elements is called indexing arrays.

Indexing in NumPy is similar to Python’s standard list indexing. In a 1D array, we can access the ith value by specifying the index of the element we need in square brackets. One important thing to remember here is that indexing in Python starts at zero.

Observe the inputs and outputs (indicated by “#>” as start marker) for the examples given below.

Note: When running the code, if you want to view the output in the console, you can add print statements if they are already not there, like at the end of this first code widget. I have omitted them to remove noise from the code so that you can fully focus on the important bits.

Get hands-on with 1200+ tech skills courses.