Indexing and Slicing

Indexing and Slicing

Array indexing refers to any use of the square brackets ([]) to index array values. There are many options to indexing, which give Numpy indexing great power. We are going to discuss the methods one by one.
1. Single element indexing: Single element indexing is about access element as we do for the 1-D array in the normal python script.
Unlike lists and tuples, Numpy arrays support multidimensional indexing for multidimensional arrays. That means that it is not necessary to separate each dimension’s index into its own set of square brackets. Let’s get it into the head through an example as one should be good at indexing, it might help a lot.
2. Slice indexing: Through slicing and striding, we can access the elements as we do for list and tuples in
python. Let’s get some illustrations of it.

How to do the slicing

The format of slicing (start index: end index: stride value) stride value-> skipping how many values

Syntax explanation for multidimensional- [index of the row, index of col]Index of row- It can be a single value or with slicing and stride (similar for the index of col)

3. Index Arrays: We can access elements of a Numpy array using another array(or any other sequence – like object that can be converted to an array, such as lists, with the exception of tuples)

How to do indexing through arrays

4. Boolean index: Boolean indexing is used when we need to do some comparisons between elements of the Numpy array. To illustrate:

How to use boolean indexing

Close Menu