How to Reference Elements in an Array in Python 


               


How to Reference Elements in an Array in Python



Python


In this article, we show how to reference elements in an array in Python.

So imagine you've created an array in Python.

How do you then reference the individual elements of the array?

For example, say you want the third element of the 2nd row. How do you reference this?

So we go through this in this article, so that you can see practical examples.

Referencing Items in a One-Dimensional Array

So let's first create a one-dimensional array.

So below is code that creates a one-dimensional array using numpy.



So, first, we must import numpy as np, since we are using numpy to create an array.

We create a one-dimensional array consisting of 4 numbers.

Referencing an element of a one-dimensional array is very similar (pretty much the same) as referencing an element of a list in Python.

The first element is at an index of 0.

The second element is at an index of 1.

The third element is at an index of 2.

The fourth element is at an index of 3.

Referencing Items in a Two-Dimensional Array

Now we will show how to reference items of a two-dimensional array. This is an array that contains rows and columns.

We will create a two-dimensional array and then show how to reference each individual item in the array.

Next, we create an array, called originalarray, that goes from 0 to 5, so an array of 6 elements.

We show the contents of this array, an array that goes from 0 to 5.

We tehn create another variable, copiedarray, and set it equal to, originalarray.copy()

We then show the contents of this copiedarray, which is composed of the same exact elements of the original array.

In other words, we have created a copy, or a duplicate, of the original array.

Now if you were to make changes to one of the arrays, it would not affect the other array, because after this point, both arrays are independent of each other.

And this is how you can create a copy of an array in Python.



So to reference an element of a two-dimensional array, the first parameter is the row that you are referencing and the second parameter is the column you are referencing. So array2[1][3] would be referencing the element of the index of row 1, column 3 (the 2nd row and the fourth column).

Another notation that you can use to reference an element of an array is to use a single opening and closing brackets.

For example, if you want to reference row 1, column 2, it can also be done with the following code shown below.



The first method is more explicit, so I would say that's the preferred way of referencing items of a two-dimensional array.

Referencing an Entire Row of an Array

Now let's say that you want to reference an entire row of an array.

You would just specify the row and not the column.

This is shown in the code below.



So to get the first row of the array, array2, we reference this by the line, array2[0]

To get the second row of the array, array2, we reference this by the line, array2[1]

To get the third row of the array, array2, we reference this by the line, array2[2]

And this is how we can reference elements of an array in Python.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...