- Array Indexing in Python | Explained With Examples
- How to Index an Array in Python?
- Example 1: Indexing 1D Arrays
- Example 2: Indexing 2D Arrays
- Example 3: Indexing 3D Arrays
- Example 4: Indexing Multidimensional Arrays
- Example 5: Negative Indexing in Python
- Example 6: Using Array Indexing to Perform Arithmetic Operation
- Conclusion
- Array Indexing in Python – Beginner’s Reference
- Getting Started with Array Indexing in Python
- Arithmetic Operations using Array Indexing
- Indexing 2D Arrays in Python
- Indexing 3D Arrays in Python
- Python Array Index (Multi-dimensional Arrays)
- Conclusion
Array Indexing in Python | Explained With Examples
In Python, array indexing means accessing the elements from the arrays using index numbers. The positive and negative indexing is used to access the element of arrays from start and end. The array indexing feature allows for more efficient manipulation and use of arrays. The index value starts from “0” for the first items and increments by “1” for each subsequent item.
This post provides a complete guide on arrays indexing using the below contents:
How to Index an Array in Python?
To index an array, the standard accessing syntax, i.e., “array[obj]” is used in Python. In this syntax, “obj” specifies the index of an array element that needs to be accessed from the arrays. For example, an “array[0]” accesses the first element/items of the array, an “array[1]” accesses the second element/items, and so on.
Let’s understand the array indexing methods using numerous examples:
Example 1: Indexing 1D Arrays
The given below example is utilized to access the element of the 1-D array using an index value:
import numpy array_value = numpy.array([45, 55, 52, 15, 25]) print(array_value[4])
- The “numpy.array()” function is utilized to create the array.
- The “array_value[]” takes the integer as an argument and accesses the specific index value.
The element value placed at the specific index has been accessed.
Example 2: Indexing 2D Arrays
The below example code is used to access the elements of 2-D arrays using the specific index value:
import numpy array_value=numpy.array([[45, 55, 23, 32],[24,35,36,45]]) print(array_value[0,2])
- The expression “array_value[0,2]” means that the element is specified using the two integers inside square brackets after the array name.
- The first integer is 0 and it represents the row index.
- The second integer is 2 and it represents the column index.
The value of the items in the “1st” row and the “3rd” column of the array object is printed successfully.
Example 3: Indexing 3D Arrays
The below code access the element of the 3-D array using the index value:
import numpy array_value= numpy.array([[[21, 32, 13], [44, 25, 46]], [[72, 84, 29], [20, 31, 15]]]) print(array_value[1, 1, 2])
- The “numpy.array()” function of the numpy module is used to create the “3-dimensional” array.
- The expression “array_value[1, 1, 2]” is used to print the value of a specific element in the array using indexing.
- First, we accept the second element of axis 0 (the second two-dimensional array).
- Secondly, the second element of axis 1 (the second row) is selected from the array.
- Lastly, the third element of axis 2 (the third column) is selected from that specific row.
The specific element value has been accessed from the “3-dimensional” array.
Example 4: Indexing Multidimensional Arrays
To store data of different types and of different sizes in a single array, the multidimensional array is used in Python. The below code is used to access the elements of the multidimensional arrays:
import numpy array_value = numpy.arange(9).reshape(3,3) print('3x3 Array: \n',array_value) print('\nElement at Row 1 and Column 2: ',array_value[1,2]) print('\nAccess a Subarray Using Slicing: \n',array_value[0:2,1:])
- The module named “numpy” is imported.
- The “numpy.arange()” function is used to create a 1D array of 9 elements from 0 to 8.
- The “reshape()” function is used to reshape it into a 2D array of 3 rows and 3 columns.
- The expression “array_value[1,2]” is used to display the element value at row index 1 and column index 2 of the array.
- The expression “array_value[0:2, 1:]” shows the subarray of the original array using slicing notation. In other words the subarray [0:2,1:] access rows from index 0 (inclusive) to index 2 (exclusive) and columns from index 1 (inclusive) to the end.
The multidimensional array has been successfully indexed using slicing notation.
Example 5: Negative Indexing in Python
The negative indices are also used to access elements/items from the end of the array. Here is an example code:
import numpy array_value = numpy.array([44, 45, 54, 32, 54]) print('Accessing Element Using Negative Index: ', array_value[-3])
- The “numpy.array()” function is utilized to create the array.
- The negative value is used to access the array element values from the end side of the array. The last element/item of the given array is considered as “-1”.
The specific element of the array has been selected from the end side using a negative index.
Example 6: Using Array Indexing to Perform Arithmetic Operation
The following example uses an arithmetic operator to perform mathematical calculations on the array element:
import numpy value=numpy.array([44, 45, 54, 32, 54]) print('Addition of Two Elements of an Array: ', value[1]+value[3]) print('\nsubtraction of Two Elements of an Array: ',value[3]-value[1]) print('\nMultiplication of Two Elements of an Array: ',value[2]*value[4]) print('\nDivision of Two Elements of an Array: ',value[2]/value[4])
- The array is created using the “numpy.array()” function.
- The element value is accessed using the square bracket notation.
- Different operations such as subtraction, addition, multiplication, and division are executed on the access element value of the array.
The arithmetic operations have been performed successfully on array elements.
Conclusion
In Python, “array indexing” is used to access the specific elements/items in an array or list using their index values. By using the index value, we can access the element value of 2-D, 3-D, and any multidimensional array. The negative indices can be used to access elements from the end side of the specified array. Multiples arithmetic operators such as +, -, *, and / can also perform calculations on the accessed elements value of the arrays. This guide presented a complete guide on array indexing using numerous examples.
Array Indexing in Python – Beginner’s Reference
Array Indexing means searching for elements in an array using the index (position) of elements for quick retrieval of information.
Getting Started with Array Indexing in Python
Python arrays are variables that consist of more than one element. In order to access specific elements from an array, we use the method of array indexing.
The first element starts with index 0 and followed by the second element which has index 1 and so on. NumPy is an array processing package which we will use further.
Let’s start this off with a few examples.
Indexing to retrieve the third element of the array
>>> import numpy as np >>> a=np.array([10,20,30,40,50]) >>> print(a[2]) 30
In the above example, there are five elements in the array. 10 has index 0, 20 has index 1, 30 has index 2, 40 has index 3 and 50 has index 4.
So to retrieve the third element of the array which is 30 we determined its index 2.
Indexing to retrieve the fifth element of the array
>>> import numpy as np >>> a=np.array([10,20,30,40,50]) >>> print(a[4]) 50
Arithmetic Operations using Array Indexing
Let’s perform arithmetic operations on individual elements of an array using indexing.
1. Adding two elements of an array using index
>>> import numpy as np >>> a=np.array([10,20,30,40,50]) >>> print(a[1]+a[3]) 60
2. Subtracting two elements of an array using index
>>> import numpy as np >>> a=np.array([10,20,30,40,50]) >>> print(a[4]-a[0]) 40
3. Multiply two elements of an array using index
>>> import numpy as np >>> a=np.array([10,20,30,40,50]) >>> print(a[2]*a[3]) 1200
4. Divide two elements of an array using index
>>> import numpy as np >>> a=np.array([10,20,30,40,50]) >>> print(a[2]/a[3]) 0.75
Indexing 2D Arrays in Python
2-Dimensional arrays in Python can be accessed using value, row, and columns. The general syntax for accessing specific elements from a 2D array is as follows:
Here, means the variable where the retrieved element from the array is stored. And [row, column] specifies the row and column index of the value.
Construct a 2D array and retrieve one element using array index.
>>> import numpy as np >>> a=np.array([[1,2,3],[4,5,6]]) >>> print(a) [[1 2 3] [4 5 6]] >>> value=a[1,2] >>> print(value) 6
Indexing 3D Arrays in Python
Following is the general syntax for accessing elements from a 3D array using index.
Syntax : array[first dimension, second dimension, third dimension]
Here the first, second and third numbers represent 1D, 2D and 3D respectively.
Construct a 3D array and retrieve one element using the array index.
>>> import numpy as np >>> a= np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]) >>> print(a[0, 1, 2]) 6
Python Array Index (Multi-dimensional Arrays)
Indexing a multi-dimensional array is quite complex. Let us start with creating a simple multidimensional array. For creating a multidimensional array we will use reshape() and arange() methods.
- The reshape() function takes a single argument that specifies the new shape of the array.
- The arange() method is used in Numpy. It takes to start and end arguments and creates a single dimension array.
>>> import numpy as np >>> arr=np.arange(10).reshape(5,2) >>> print(arr) [[0 1] [2 3] [4 5] [6 7] [8 9]]
>>> import numpy as np >>> arr=np.arange(12).reshape(2,2,3) >>> print(arr[0:3]) [[[ 0 1 2] [ 3 4 5]] [[ 6 7 8] [ 9 10 11]]] >>> print(arr[1:5:2. 3]) [[[6 7 8]
Conclusion
This was in brief about array indexing in the Python programming language. Hope this article proves helpful. You can learn more about array slicing in Python here.