How to do Indexing and Slicing of 1-D NumPy array?

In this article, we will learn about the 1-D NumPy array. NumPy array supports 0 based indexing and each element can be accessed by indexing. In this article, we will understand the indexing and slicing of this 1-D array in NumPy.So let us begin with our examples:

1D Arrays or One-dimensional arrays

In our example we will take an array of numbers and we will try to understand the indexing concept.

import numpy as np
lists = ([1,2,3,4,5,6,7,8,9,10])
array_one = np.array(lists)
print("Full array is :",array_one)

Output:

Full array is : [ 1 2 3 4 5 6 7 8 9 10]

Accessing element at specific Index

We can access the element at 4th index by using square brackets with index 4.

import numpy as np

lists = ([1,2,3,4,5,6,7,8,9,10])
array_one = np.array(lists)

print("Value at array[4] is :",array_one[4])

Output:

Value at array[4] is : 5

Access Element from the end in Reverse

We can access the elements from the end in reverse also. To do that we will need to use the negative index with in square brackets.

import numpy as np

lists = ([1,2,3,4,5,6,7,8,9,10])
array_one = np.array(lists)

print("Value at array[-4] is :",array_one[-4])

Output:

Value at array[-4] is : 7

Access Range of elements

We can access elements from 2 to 5 starting from starting element ,We can specify 2:5 in square bracket which means start from 2 but stop at index 4, index 5 is not included

import numpy as np

lists = [1,2,3,4,5,6,7,8,9,10]
array_one = np.array(lists)

print("Slice of array from index 2 to 7 is :",array_one[2:5])

Output:

Slice of array from index 2 to 7 is : [3 4 5]

When start and end indexes are not specified

If you do not specify the start and end index then whole elements of array will be accessed.

import numpy as np

lists = [1,2,3,4,5,6,7,8,9,10]
array_one = np.array(lists)

print("no start and end index :",array_one[:])

Output

no start and end index : [ 1  2  3  4  5  6  7  8  9 10]

We can access from index 1 from the beginning and go to the end side and stop at -2, -3 is not included

import numpy as np

lists = [1,2,3,4,5,6,7,8,9,10]
array_one = np.array(lists)
print("Slice of array from index 1 to -3 is :",array_one[1:-3])

Output:

Slice of array from index 1 to -3 is : [2 3 4 5 6 7]

Specified start Index Only

We can access array from index 2 to end of array.

import numpy as np

lists = [1,2,3,4,5,6,7,8,9,10]
array_one = np.array(lists)
print("Slice of array from index 2 to end is :",array_one[2:])

Output:

Slice of array from index 2 to end is : [ 3 4 5 6 7 8 9 10]

Specified End Index only

We can access array from begin to index 7 , 7th index is not included

import numpy as np

lists = [1,2,3,4,5,6,7,8,9,10]
array_one = np.array(lists)
print("Slice of array from begin to index 7 is :",array_one[:7])

Output:

Slice of array from begin to index 7 is : [1 2 3 4 5 6 7]

We can access from begin to index 7 with step size of 2.

import numpy as np

lists = [1,2,3,4,5,6,7,8,9,10]
array_one = np.array(lists)
print("Slice of array from begin to index 7 and step of 2 is :",array_one[:7:2])

Output:

Slice of array from begin to index 7 and step of 2 is : [1 3 5 7]

We can also Step by -1 but from end to beginning.This can be used to reverse an array

import numpy as np

lists = [1,2,3,4,5,6,7,8,9,10]
array_one = np.array(lists)
print("Slice of array from begin to end with step of -1 :",array_one[::-1])

Output:

Slice of array from begin to end with step of -1 : [10 9 8 7 6 5 4 3 2 1]