PY-401 ยท NumPy foundations
Indexing and slicing arrays
โฑ 9 min ๐ Real Python 3 ๐ฌ Animated โ Completed
โก The 30-second answer
NumPy arrays are indexed with square brackets like lists, but a 2-D array takes a row and a column in one pair: sales[1, 2] is row 1, column 2. A colon means "everything", so sales[:, 0] is the whole first column. Slices give you a view of the same data, not a copy.
- Read one value with [row, col]
- Take a whole row or column
- Know when a slice changes the original
Try it
Watch it come alive, run it yourself, then prove it. Real Python, right on this page.
Watch Python run the example. Every variable is a jar; watch the values change and the output appear.
import numpy as np# rows = stores A, B, C columns = Mon, Tue, Wedsales = np.array([[10, 12, 9],[14, 11, 15],[8, 13, 10]])print(sales[0]) # store A, every dayprint(sales[1, 2]) # store B on Wednesdayprint(sales[:, 0]) # Monday, every storeprint(sales[1:, 1:]) # stores B and C, Tue and Wed
Change anything and press Run (or Ctrl+Enter). Then open Watch it and press Watch my code.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Expected output
[10 12 9]
15
[10 14 8]
[[11 15]
[13 10]] ๐ฏ marks has one row per student (Asha, Ben, Chen) and one column per test. Set ben to Ben's whole row and last_test to the last column for every student.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Need a hint?
Ben is row 1: marks[1]. The last column of every row: marks[:, -1]
- The problemYour manager sends a sheet: 3 stores, 3 days of sales. "How did store B do on day 3? And what did every store sell on Monday?" Two questions, two short indexes.
- The Python idea
a[i] # 1-D: one valueNumPy arrays are indexed with square brackets like lists, but a 2-D array takes a row and a column in one pair:sales[1, 2]is row 1, column 2. - In real lifeAn image is a 2-D array of pixels. Cropping a profile photo to a square is one slice:
photo[100:400, 200:500].
Start with a question
Your manager sends a sheet: 3 stores, 3 days of sales. "How did store B do on day 3? And what did every store sell on Monday?" Two questions, two short indexes.
Every analysis starts by picking the part of the data you care about: one customer, one month, one column.
Explain it like I'm 10
Think of a chocolate bar with rows and columns of squares. [1, 2] points at one square. [:, 0] snaps off the whole first column, top to bottom.
The technical explanation
Indexes start at 0, and negative indexes count from the end, exactly like lists: a[-1] is the last value.
For 2-D arrays write a[row, col]. a[0] alone is the whole first row.
: means all, so a[:, 2] is column 2 of every row, and a[1:, 1:] is everything from row 1 and column 1 onward.
Slicing does not copy. The slice and the original share memory, which is fast but means writes go through. a[0].copy() makes an independent array.
Syntax
a[i] # 1-D: one value
a[row, col] # 2-D: one value
a[row] # whole row
a[:, col] # whole column
a[r1:r2, c1:c2] Real-world scenario
An image is a 2-D array of pixels. Cropping a profile photo to a square is one slice: photo[100:400, 200:500].
Test yourself
5 questions. Get 4 right and this lesson is marked complete.
Pass the quiz to complete this lesson.
๐ Every lesson counts toward your certificate Next: Vectorised maths and broadcasting โQuestions people ask
What is the difference between a[1][2] and a[1, 2] in NumPy?
They return the same value, but a[1, 2] does it in one step. a[1][2] first builds row 1 and then indexes it, which is slower and does not work for column selections like a[:, 2].
Does slicing a NumPy array copy it?
No. A basic slice returns a view that shares memory with the original, so changing the view changes the original. Call .copy() when you need an independent array.
Interview questions
How this topic comes up in Python interviews. Answer out loud first, then open the model answer.
beginner What is the difference between a[1][2] and a[1, 2] in NumPy?
They return the same value, but a[1, 2] does it in one step. a[1][2] first builds row 1 and then indexes it, which is slower and does not work for column selections like a[:, 2].
beginner Does slicing a NumPy array copy it?
No. A basic slice returns a view that shares memory with the original, so changing the view changes the original. Call .copy() when you need an independent array.
intermediate a = np.array([[1, 2], [3, 4]]). What is a[1, 0]?
3. Row 1 is [3, 4]; column 0 of it is 3.
intermediate What does a[:, 1] select?
Column 1 of every row. The colon means every row, and 1 picks column 1.
Quick revision
Remember
- Read one value with [row, col]
- Take a whole row or column
- Know when a slice changes the original
- A slice is a window onto the same numbers.
week = sales[0]thenweek[0] = 99changessalestoo. Use.copy()when you want your own copy.
Syntax
a[i] # 1-D: one value
a[row, col] # 2-D: one value
a[row] # whole row
a[:, col] # whole column
a[r1:r2, c1:c2] Official documentation
- Indexing on ndarrays (docs.python.org)