Python Array Indexing and Slicing
What are Arrays?
Arrays are a collection of similar items stored in contiguous memory locations. For instance, an array of integers can store only integers, and an array of chars can only store characters. The storing of these items in the adjacent memory blocks helps in accessing the items quickly. The users can only use the index numbers to access that particular index or item of an array directly. Moreover, these can be multi-dimensional and can store a large amount of data.
Arrays in Python
Unlike various other higher-level languages, python does not have built-in arrays. However, the users can use python lists or import libraries such as NumPy containing code that works like arrays.
Note: The users will be using the NumPy library for arrays in this tutorial.
Array Indexing
The array indexing means accessing a row’s particular item, row, column, or plane in the case of multidimensional arrays. Indices are primarily integers that tell the position of an element in the given array. Furthermore, it is essential to note that the arrays usually start from the zeroth index and so forth.
Indexing of 1D array
Below is the example code to create and index a one-dimensional array in python:
Import numpy as np arr = np.array([2, 4, 6, 8, 10]) print( arr ) #prints 2,4,6,8,10 print( arr[0] ) #prints 2 print( arr[1] ) #prints 4 print( arr[4]) #prints 10
In the above example, the user imports the NumPy library to create an array in python. He then calls an array method of the library to create an array with given elements. In this example, he is creating an array of even numbers. Now, he uses the print function to print the values of the array. The “arr” variable refers to the whole array and will print all the array elements on the user’s screen. The user needs to define the index number to access the particular array element.
Indexing of a Multidimensional Array
Below is an example code to create and index a three-dimensional array in python:
importing numpy as np arr = np.array([ [ [1,2,3], [2,3,4], [3,4,5] ], [ [4,5,6], [5,6,7], [6,7,8] ], [ [7,8,9], [8,9,7], [3,7,9] ] ]) print(arr[0,1,2]) #prints 3
In the above example, the user creates a three-dimensional array with three rows and three columns. The users can visualize it as three matrices with three rows and columns each or a cube with three sides. In a 3D array, the first entry refers to the matrix. Similarly, the second entry refers to the row and the third to its column. Therefore, in the above example, [0,1,2] means zeroth matrix, first row, and second column.
Slicing a Python Array
Slicing a python array means getting items from a particular index to another particular index—for instance, printing elements from index two to seven from an array containing ten elements.
The users can similarly slice the python lists. However, slicing arrays are different from slicing lists so that the users can slice in multiple dimensions in arrays. Moreover, the list slicing returns a new array, but the slice in the array returns the view of the same array.
Slicing an Array
Below is an example code of slicing a one-dimension array in python:
import numpy as nump arr= nump.array( [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 ] ) slice = arr[0:5] print(slice) print( arr[2 : 4] ) #prints [3,4] print( arr[ : 4] ) #prints [1,2,3,4] print( arr[-4 : -2] ) #prints [7,8]
Slicing from one Index to Another
In the above example, the arr[0:5] means to slice the “arr” array from the zeroth index to the fifth index (not including the fifth). So, it returns a view of the first five elements and stores it in the variable slice. Here, the slice variable and arr[0:5] are the same; therefore, making a change in the “slice” variable will also affect the original array “arr.”
Slicing from Start to a Particular Index
In the above example, arr[: 4] returns the array view from the start to the fourth index (not including the fourth). Similarly, the users can write arr[4: ] to get the array from the fourth index to the end.
Negative Slicing
In the above coding example, arr[-4:-2] returns an array starting from the fourth index from the end to the second index from the end.
Other useful articles:
- OOP in Python
- Python v2 vs Python v3
- Variables, Data Types, and Syntaxes in Python
- Operators, Booleans, and Tuples
- Loops and Statements in Python
- Python Functions and Modules
- Regular Expressions in Python
- Python Interfaces
- JSON Data and Python
- Pip and its Uses in Python
- File Handling in Python
- Searching and Sorting Algorithms in Python
- System Programming (Pipes &Threads etc.)
- Database Programming in Python
- Debugging with Assertion in Python
- Sockets in Python
- InterOp in Python
- Exception Handling in Python
- Environments in Python
- Foundation of Data Science
- Reinforcement Learning
- Python for AI
- Applied Text Mining in Python
- Python Iterations using Libraries
- NumPy vs SciPy
- Python Array Indexing and Slicing
- PyGame
- PyTorch
- Python & Libraries