How to Reference Elements in an Array in Python
Prerequisites: Numpy
Elements of an array can be referenced like a regular python array. Since python internally doesn’t support arrays, here whenever we use the term array we are referring to pythons list that can be used to build an array of any required dimension. Besides this, Python’s NumPy module also provides a container named ‘array’ for storing collections of data. In this article, we will talk about how to reference elements in a Python array as well as numpy array in Python.
- For array referencing only the index of the required element has to be passed to the name of the array.
Syntax:
array_name[index]
- For referencing using numpy array, first an array is created using numpy’s array function then it is referenced like a regular array.
Syntax:
np.array([array elements])
Implementation using both methods is given below for various cases:
Example 1: Referencing items in a 1-D array
Example with Python Array
Python3
arr = [ 4 , 6 , 3 , 9 , 2 ]
first_element = arr[ 0 ]
second_element = arr[ 1 ]
third_element = arr[ 2 ]
fourth_element = arr[ 3 ]
fifth_element = arr[ 4 ]
print ( "First Element =" , first_element)
print ( "Second Element =" , second_element)
print ( "Third Element =" , third_element)
print ( "Fourth Element =" , fourth_element)
print ( "Fifth Element =" , fifth_element)
|
Output:
Example with Python’s numpy module’s array
Python3
import numpy as np
arr = np.array([ 4 , 6 , 3 , 9 , 2 ])
first_element = arr[ 0 ]
second_element = arr[ 1 ]
third_element = arr[ 2 ]
fourth_element = arr[ 3 ]
fifth_element = arr[ 4 ]
print ( "First Element =" , first_element)
print ( "Second Element =" , second_element)
print ( "Third Element =" , third_element)
print ( "Fourth Element =" , fourth_element)
print ( "Fifth Element =" , fifth_element)
|
Output:
Example 2: Referencing Items in a 2-D Array
Example with Python Array
Python3
arr = [[ 4 , 6 , 3 ],
[ 5 , 9 , 2 ],
[ 1 , 8 , 7 ]]
first_row_second_column = arr[ 0 ][ 1 ]
second_row_first_column = arr[ 1 ][ 0 ]
third_row_third_column = arr[ 2 ][ 2 ]
print ( "First Row Second Column =" , first_row_second_column)
print ( "Second Row First Column =" , second_row_first_column)
print ( "Third Row Third Column =" , third_row_third_column)
|
Output:
Example with Python’s numpy module’s array
Python3
import numpy as np
arr = np.array([[ 4 , 6 , 3 ],
[ 5 , 9 , 2 ],
[ 1 , 8 , 7 ]])
first_row_second_column = arr[ 0 ][ 1 ]
second_row_first_column = arr[ 1 ][ 0 ]
third_row_third_column = arr[ 2 ][ 2 ]
print ( "First Row Second Column =" , first_row_second_column)
print ( "Second Row First Column =" , second_row_first_column)
print ( "Third Row Third Column =" , third_row_third_column)
|
Output:
Example 3: Referencing Items in a3-D Array
Example with Python array
Python3
arr = [[[ 4 , 6 , 3 ], [ 2 , 6 , 8 ], [ 3 , 5 , 12 ]],
[[ 32 , 11 , 4 ], [ 23 , 53 , 89 ], [ 19 , 17 , 10 ]],
[[ 14 , 22 , 52 ], [ 56 , 43 , 99 ], [ 20 , 37 , 32 ]]]
first_second_second = arr[ 0 ][ 1 ][ 1 ]
second_first_third = arr[ 1 ][ 0 ][ 2 ]
third_third_first = arr[ 2 ][ 2 ][ 0 ]
print ( "First Second Second Value =" , first_second_second)
print ( "Second First Third Value =" , second_first_third)
print ( "Third Third First Value =" , third_third_first)
|
Output:
Example with Python’s numpy module’s array
Python3
import numpy as np
arr = np.array([[[ 4 , 6 , 3 ], [ 2 , 6 , 8 ], [ 3 , 5 , 12 ]],
[[ 32 , 11 , 4 ], [ 23 , 53 , 89 ], [ 19 , 17 , 10 ]],
[[ 14 , 22 , 52 ], [ 56 , 43 , 99 ], [ 20 , 37 , 32 ]]])
first_second_second = arr[ 0 ][ 1 ][ 1 ]
second_first_third = arr[ 1 ][ 0 ][ 2 ]
third_third_first = arr[ 2 ][ 2 ][ 0 ]
print ( "First Second Second Value =" , first_second_second)
print ( "Second First Third Value =" , second_first_third)
print ( "Third Third First Value =" , third_third_first)
|
Output:
Example 4: Referencing an Entire Row of an Array
Example with Python array
Python3
arr = [[ 4 , 6 , 3 ],
[ 5 , 9 , 2 ],
[ 1 , 8 , 7 ]]
first_row = arr[ 0 ]
second_row = arr[ 1 ]
third_row = arr[ 2 ]
print ( "First Row =" , first_row)
print ( "Second Row =" , second_row)
print ( "Third Row =" , third_row)
|
Output:
Example with Python’s numpy module’s array
Python3
import numpy as np
arr = np.array([[ 4 , 6 , 3 ],
[ 5 , 9 , 2 ],
[ 1 , 8 , 7 ]])
first_row = arr[ 0 ]
second_row = arr[ 1 ]
third_row = arr[ 2 ]
print ( "First Row =" , first_row)
print ( "Second Row =" , second_row)
print ( "Third Row =" , third_row)
|
Output:
Last Updated :
03 Jan, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...