# Python: Select an Element or Sub Array by Index From a Numpy Array

## Select an element or subarray by index from a Numpy array

In this article, we will discuss how we can access elements of numpy array using indexes and how to access subarray of Numpy array using slicing or range of indexes.

### Access element of Numpy array using indexes

As we all know array is a data structure in which elements are stored in a contiguous memory location. Hence it is easy to access array elements using an index. The same is with the case of the Numpy array. We can access elements of the Numpy array using indexes. As we implement this in python so we can access array elements using both positive and negative indexes.

Positive index starts from 0 and it used to access the first element and using index 1,2,3………. we can access further elements. Negative index start from -1 and it used to access the last element and using index -2,-3,-4……… we can access furthermost elements. Let see this with the help of an example.

import numpy as np
#creating Numpy array
npArray=np.array([1, 2, 3, 4, 5,6,7,8,9,10])
print(npArray)
#np[0] acess first element
print(npArray[0])
#np[-1] acess last element
print(npArray[-1])
#np[3] access 4th element from start
print(npArray[3])
#np[-3] access 3rd element from last
print(npArray[-3])

Output

[ 1  2  3  4  5  6  7  8  9 10]
1
10
4
8

### Access subarray of Numpy array using slicing or range of indexes

When we study the list in python we see that we can access the subarray of the list using slicing. Its syntax looks like this:

Suppose L is a list we can access the subarray of the list using L[a:b] where a denote starting index while b-1 denotes the last index of the subarray. In a similar way, we can implement this concept in a Numpy array.

Now we see different structures of slicing for positive index

1) L[a:b]-> a denote starting index of numpy array and b-1 denotes last index of numpy array.

import numpy as np
#creating Numpy array
npArray=np.array([1, 2, 3, 4, 5,6,7,8,9,10])
print(npArray)
#Here we start from 3rd index and stop at 5th index
print(npArray[3:6])

Output

[ 1  2  3  4  5  6  7  8  9 10]
[4 5 6]

2) L[:b]-> Here a becomes starting index of the whole array i.e a is equal to zero and b-1 denotes the last index of the numpy array.

import numpy as np
#creating Numpy array
npArray=np.array([1, 2, 3, 4, 5,6,7,8,9,10])
print(npArray)
#Here we start from 0th index and stop at 5th index
print(npArray[:6])

Output

[ 1  2  3  4  5  6  7  8  9 10]
[1 2 3 4 5 6]

3) L[a:]-> a denote starting index of the numpy array and b becomes the last index of the whole array.

import numpy as np
#creating Numpy array
npArray=np.array([1, 2, 3, 4, 5,6,7,8,9,10])
print(npArray)
#Here we start from 2nd index and stop at last index
print(npArray[2:])

Output

[ 1  2  3  4  5  6  7  8  9 10]
[ 3  4  5  6  7  8  9 10]

4) L[a:b:c] -> a denote starting index of numpy array and b-1 denotes the last index of numpy array and c-1 denote how many elements we have to skip in between. The default value of c is 1.

import numpy as np
#creating Numpy array
npArray=np.array([1, 2, 3, 4, 5,6,7,8,9,10])
print(npArray)
#Here we start from 2nd index and stop at sixth index and leave 1 element in  between
print(npArray[2:7:2])

Output

[ 1  2  3  4  5  6  7  8  9 10]
[3 5 7]

5) L[a::c] -> a denote starting index of the numpy array and b becomes the last index of the whole array and c-1 denotes how many elements we have to skip in between. The default value of c is 1.

import numpy as np
#creating Numpy array
npArray=np.array([1, 2, 3, 4, 5,6,7,8,9,10])
print(npArray)
#Here we start from 2nd index and stop at last index and leave 1 element in  between
print(npArray[2::2])

Output

[ 1  2  3  4  5  6  7  8  9 10]
[3 5 7 9]

Now we see different structures of slicing for the Negative index

1) L[a:b:c]-> a denote starting index of numpy array and b denotes last index of numpy array.Here c=-1 means we have to skip 0 elements,c=-2 means we have to skip 1 element, and so on

import numpy as np
#creating Numpy array
npArray=np.array([1, 2, 3, 4, 5,6,7,8,9,10])
print(npArray)
#Here we start from 1st index from last and stop at 5th index from last
print(npArray[-1:-5:-1])

Output

[ 1  2  3  4  5  6  7  8  9 10]
[10  9  8  7]

2) L[a::c]-> a denote starting index of the numpy array and b becomes the last index of the whole array. Here c=-1 means we have to skip 0 elements,c=-2 means we have to skip 1 element, and so on.

import numpy as np
#creating Numpy array
npArray=np.array([1, 2, 3, 4, 5,6,7,8,9,10])
print(npArray)
#Here we start from last index and stop at 5th index from last leaving 1 element
print(npArray[:-5:-2])

Output

[ 1  2  3  4  5  6  7  8  9 10]
[10  8]

3) 5) L[a::c] -> a denote starting index of the numpy array and b becomes the last index of the whole array. Here c=-1 means we have to skip 0 elements,c=-2 means we have to skip 1 element, and so on.

import numpy as np
#creating Numpy array
npArray=np.array([1, 2, 3, 4, 5,6,7,8,9,10])
print(npArray)
#Here we start from second index from last and stop at last index from last leaving 1 element
print(npArray[-2::-2])

Output

[ 1  2  3  4  5  6  7  8  9 10]
[9 7 5 3 1]

So these are the methods select an element or subarray by index from a Numpy array.