Indexing and Slicing Numpy Arrays

Indexing and Slicing Numpy Arrays

Hello Guys👋

In the previous article, we have learned different methods of creating Numpy Arrays. In this article, we will see how to perform indexing and slicing on Numpy Arrays.

Let's get started with this article👍.

Indexing & Slicing in 1D Arrays

Both indexing and slicing are similar to Python Lists.

Indexing :

  • Zero-based Indexing - index of 1st element is 0
  • Supports Negative Indexing - Index of last element is -1

To get more clarity, Let's see this in an example now.

numbers_array = np.array([1, 4, 7, 0, 55, 79, 3])
first_number = numbers_array[0]
third_number = numbers_array[2]
last_number = numbers_array[-1]
third_from_last_number = numbers_array[-3]

print("Array : ", numbers_array)
print("first_number : ", first_number)
print("third_number : ", third_number )
print("last_number : ", last_number )
print("third_from_last_number : ", third_from_last_number )

By now you may have guessed what it prints👏. Let's see.

Array :  [ 1  4  7  0 55 79  3]
first_number :  1
third_number :  7
last_number :  3
third_from_last_number :  55

Slicing :

  • Slice with the [start:stop] syntax outputs elements in the interval [start, stop)
  • Slice with the [start:stop:step] syntax outputs elements in the interval [start, stop) with a step size of step

Let's see various types of slicing

numbers_array = np.array([1, 4, 7, 0, 55, 79, 3, 45, 28])
  • General Method :

    • numbers_array[3:6] returns the elements having index from 3 to 5 (excluding 6)
    • numbers_array[2:8:2] returns the elements having index from 2 to 8 with a step of 2.
  • Using Negative indices :

    • numbers_array[1:-2] returns the elements having index from 1 till last but 2nd number.
    • numbers_array[1:-2:2] returns the elements having index from 1 till last but 2nd number with a step of 2.
    • numbers_array[-1:0:-2] returns elements from last to first
    • numbers_array[-2:1:2] returns elements from last but 2nd till the element with index 1 with a step of 2.
  • Omitting Start and Stop Indices :

    • numbers_array[ :4] returns the first 4 elements.
    • numbers_array[4: ] returns the elements having index from 4 to end of array.
    • numbers_array[-3: ] returns the last 3 elements.
    • numbers_array[1: :2] returns all the elements from 2nd element till the end with the step of 2.
    • numbers_array[ : :2] returns all the elements from start to end with a step of 2.

Modifying Slices :

In NumPy, slices can be assigned

  • a constant or
  • an array of the right shape.

For example

  • Assigning constant to a Slice :
    • numbers_array[1:4] = 5 This changes all the values from index 1 to 3 to 5.
  • Assigning an array of right shape to a Slice
    • numbers_array[2:6] = np.array([1, 2, 3, 4]) This replaces the slice with the new array assigned.

Important Note : In NumPy slicing returns a view of the original array, instead of creating a new array. (Assignments also return the view only)

  • This makes slicing numpy arrays very fast.
  • This also means modifying a slice, modifies the underlying array as well.

To create a new array from the given Numpy array, we have to use numbers_array.copy() method. This creates a duplicate array and the changes made to this will not modify the original array.

Indexing & Slicing in 2D Arrays

Indexing :

Again same as 1D arrays,

  • Zero-based Indexing - indexing starts with 0
array_2d = np.array(['a', 'b', 'c', 'd'],
                    ['e', 'f', 'g', 'h'],
                    ['i', 'j', 'k', 'l']])

Let's how we can access rows, columns, and elements.

  • array_2d[i] gives the i+1 th row, like array_2d[0] gives the first row.
  • array_2d[ :,i] gives the i+1 th column, like array_2d[:, 0] gives the first column.
  • 'array_2d[a:b]gives the element ina+1 th rowandb+1 th column`, like array_2d[0][0] gives the element in first row and first column. (1st element in 1st row)

Slicing :

array_2d = np.array(['a', 'b', 'c', 'd'],
                    ['e', 'f', 'g', 'h'],
                    ['i', 'j', 'k', 'l']])

We will see possible ways of slicing 2d arrays.

  • array_2d[1:3] gives the complete rows having index from 1 till 3 (excluding 3) same as in 1d arrays.
  • `array_2d[1:3, 2:4] gives the elements in rows having index 1 to 3 and columns having index 2 to 4.
  • array_2d[ :, 0:4:2] gives elements in all rows, columns having indices from 0 to 4 with a step of 2.

We also can modify the elements in 2d arrays same as in 1d arrays.

🖐☝☝Conclusion

Finally, In this article we learned how to perform indexing ad slicing on Numpy Arrays (both 1D and 2D arrays)

What's Next ⏭?

Thank you for reading so far🙏. I hope you enjoyed following it and understood how to do indexing and slicing on Numpy Arrays. I highly recommend you to try these methods.

In the next part, we will discuss the Advanced Indexing and Slicing methods of Numpy Arrays in detail and then Operations on Arrays !! So Stay Tuned👀👀!!!!

Do share your valuable suggestions and your honest feedback, also Constructive Criticism. If you have any comments, questions, or concerns, Do comment them and I am more than happy to take them💬.

Happy Learning 😊👍!!

Did you find this article valuable?

Support Sree Gayathri Siddamsetti by becoming a sponsor. Any amount is appreciated!