# 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.

#### Single element indexing

`import numpy as np`

```
##Let's first create 1-D array
x = np.arange(10)
print(x)
print(x[0])
```

`print(x[1])`

#### Converting to multidimensional array

`x.shape = (2,5)`

```
## Now it is multidimensional array of shape-> (2,5)
print(x)
print(x[0])
```

#### See the different method for indexing element in multidimensional

```
print(x[1,2])
print(x[1][2])
```

2.

**Slice indexing:**Through slicing and striding, we can access the elements as we do for list and tuples inpython. 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

```
import numpy as np
x = np.arange(10)
print(x[2:5])
print(x[2:5:2])
```

#### For multidimensional array

```
y = np.arange(35).reshape(5,7)
print(y)
```

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

```
## Simple without stride and slice
print(y[1,2])
```

#### Using “:” will go from start to end

```
## Without stride
print("This is without stride-:")
print(y[:])
print("\n")
## With stride
print("This is with the stride of 3-:")
print(y[::3])
```

```
## It will extract rows from 1 to 4 and for all columns
print(y[1:5,:])
```

```
## If we want to choose particular columns
print(y[1:5:2,::3])
```

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

```
import numpy as np
x = np.arange(10,1,-1)
print(x)
```

`print(x[np.array([3,3,4,7])])`

#### Negative indexing is the same as work with single indexes

`x[np.array([3,3,-3,8])]`

#### Index out of range will give an error

```
x[np.array([3,3,19,8])]
```

`x[np.array([[1,1],[2,3]])]`

4.

**Boolean index**: Boolean indexing is used when we need to do some comparisons between elements of Numpy array. To illustrate:#### How to use boolean indexing

`import numpy as np`

`x = np.arange(35)`

`print(x)`

#### If we want elements having value >20

```
## Comparision operator should be used first and it will return boolean array.
b = x>20
print(b)
## It will return elements which are having True value in b
print(x[b])
## We can merge these 2 steps in one step
print(x[x>20])
```