🚀
Numpy
01 Numpy Indexing and Selection
++++
Data Science
May 2026×Notebook lesson

Notebook converted from Jupyter for blog publishing.

01-NumPy-Indexing-and-Selection

Driptanil Datta
Driptanil DattaSoftware Developer

NumPy Indexing and Selection

In this lecture we will discuss how to select elements or groups of elements from an array.

import numpy as np
#Creating sample array
arr = np.arange(0,11)
#Show
arr
RESULT
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10])

Bracket Indexing and Selection

The simplest way to pick one or some elements of an array looks very similar to python lists:

#Get a value at an index
arr[8]
RESULT
8
#Get values in a range
arr[1:5]
RESULT
array([1, 2, 3, 4])
#Get values in a range
arr[0:5]
RESULT
array([0, 1, 2, 3, 4])

Broadcasting

NumPy arrays differ from normal Python lists because of their ability to broadcast. With lists, you can only reassign parts of a list with new parts of the same size and shape. That is, if you wanted to replace the first 5 elements in a list with a new value, you would have to pass in a new 5 element list. With NumPy arrays, you can broadcast a single value across a larger set of values:

#Setting a value with index range (Broadcasting)
arr[0:5]=100
 
#Show
arr
RESULT
array([100, 100, 100, 100, 100,   5,   6,   7,   8,   9,  10])
# Reset array, we'll see why I had to reset in  a moment
arr = np.arange(0,11)
 
#Show
arr
RESULT
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10])
#Important notes on Slices
slice_of_arr = arr[0:6]
 
#Show slice
slice_of_arr
RESULT
array([0, 1, 2, 3, 4, 5])
#Change Slice
slice_of_arr[:]=99
 
#Show Slice again
slice_of_arr
RESULT
array([99, 99, 99, 99, 99, 99])

Now note the changes also occur in our original array!

arr
RESULT
array([99, 99, 99, 99, 99, 99,  6,  7,  8,  9, 10])

Data is not copied, it's a view of the original array! This avoids memory problems!

#To get a copy, need to be explicit
arr_copy = arr.copy()
 
arr_copy
RESULT
array([99, 99, 99, 99, 99, 99,  6,  7,  8,  9, 10])

Indexing a 2D array (matrices)

The general format is arr_2d[row][col] or arr_2d[row,col]. I recommend using the comma notation for clarity.

arr_2d = np.array(([5,10,15],[20,25,30],[35,40,45]))
 
#Show
arr_2d
RESULT
array([[ 5, 10, 15],
       [20, 25, 30],
       [35, 40, 45]])
#Indexing row
arr_2d[1]
RESULT
array([20, 25, 30])
# Format is arr_2d[row][col] or arr_2d[row,col]
 
# Getting individual element value
arr_2d[1][0]
RESULT
20
# Getting individual element value
arr_2d[1,0]
RESULT
20
# 2D array slicing
 
#Shape (2,2) from top right corner
arr_2d[:2,1:]
RESULT
array([[10, 15],
       [25, 30]])
#Shape bottom row
arr_2d[2]
RESULT
array([35, 40, 45])
#Shape bottom row
arr_2d[2,:]
RESULT
array([35, 40, 45])

More Indexing Help

Indexing a 2D matrix can be a bit confusing at first, especially when you start to add in step size. Try google image searching NumPy indexing to find useful images, like this one:

Image source: http://www.scipy-lectures.org/intro/numpy/numpy.html (opens in a new tab)

Conditional Selection

This is a very fundamental concept that will directly translate to pandas later on, make sure you understand this part!

Let's briefly go over how to use brackets for selection based off of comparison operators.

arr = np.arange(1,11)
arr
RESULT
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])
arr > 4
RESULT
array([False, False, False, False,  True,  True,  True,  True,  True,
        True])
bool_arr = arr>4
bool_arr
RESULT
array([False, False, False, False,  True,  True,  True,  True,  True,
        True])
arr[bool_arr]
RESULT
array([ 5,  6,  7,  8,  9, 10])
arr[arr>2]
RESULT
array([ 3,  4,  5,  6,  7,  8,  9, 10])
x = 2
arr[arr>x]
RESULT
array([ 3,  4,  5,  6,  7,  8,  9, 10])

Great Job!

Drip

Driptanil Datta

Software Developer

Building full-stack systems, one commit at a time. This blog is a centralized learning archive for developers.

Legal Notes
Disclaimer

The content provided on this blog is for educational and informational purposes only. While I strive for accuracy, all information is provided "as is" without any warranties of completeness, reliability, or accuracy. Any action you take upon the information found on this website is strictly at your own risk.

Copyright & IP

Certain technical content, interview questions, and datasets are curated from external educational sources to provide a centralized learning resource. Respect for original authorship is maintained; no copyright infringement is intended. All trademarks, logos, and brand names are the property of their respective owners.

System Operational

© 2026 Driptanil Datta. All rights reserved.