Array Indexing

Array indexing is accessing array elements. In Numpy, access an array element using the index number. The 0th index is element 1 and the flow goes on as shown below:

  •   index 0 – element 1
  •   index 1 – element 2
  •   index 2 – element 3
  •   index 3 – element 4

In this lesson, we will cover the following topics to understand Array Indexing in NumPy:

  • Access elements from a 1D Array
  • Access elements from a 2D Array
  • Access elements from a 3D Array
  • Access elements from the last with Negative Indexing

Let us begin with accessing elements from a One-Dimensional i.e. 1D array:

Access elements from a One-Dimensional array

The following are some examples to access specific elements from a 1D array:

Example: Access the 1st element (index 0) from a One-Dimensional array

Output

Example: Access the 4th element (index 3) from a One-Dimensional array

Output

Access elements from a Two-Dimensional array

Accessing elements work as a matrix in a 2D Array i.e.

Following are some examples to access specific elements from a 2D array:

Example: Accessing 1st dimension elements from a 2D array

Output

Example: Accessing 2nd dimension elements from a 2D array

Output

Access elements from a Three-Dimensional Array

Following are some examples to access specific elements from a 3D array:

Example1

Output

Example2

Output

Access the array from the last with Negative Indexing

Arrays can be accessed with negative indexing. This gives the last element.

Example 1: Access the last element from a 1D array with negative indexing

Output

Example 2: Access the last element from a 2D array with negative indexing

Output

Let us see another example:

Output

If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.

For Videos, Join Our YouTube Channel: Join Now


Read More:

NumPy Tutorial
NumPy - Array Slicing
Studyopedia Editorial Staff
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading