Lesson 2 of 23
โฎPrev Nextโฏ

PY-401 ยท NumPy foundations

Indexing and slicing arrays

โฑ 9 min ๐Ÿ Real Python 3 ๐ŸŽฌ Animated

2of 23

Your note

My notes for this lesson

Select any text in the lesson to highlight it or add a note. Saved in this browser.

    No highlights yet. Select a sentence in the lesson, then pick a colour or add a note.

    All my bookmarks and notes

    Reading settings

    For every lesson, saved in this browser.

    Text size
    Line spacing
    Font

    With IaaS you rent the virtual machine. Microsoft looks after the building, power and hardware; you patch the operating system.

    โšก 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.

    Indexing and slicing arrays, running
    1. import numpy as np
    2. # rows = stores A, B, C columns = Mon, Tue, Wed
    3. sales = np.array([[10, 12, 9],
    4. [14, 11, 15],
    5. [8, 13, 10]])
    6. print(sales[0]) # store A, every day
    7. print(sales[1, 2]) # store B on Wednesday
    8. print(sales[:, 0]) # Monday, every store
    9. print(sales[1:, 1:]) # stores B and C, Tue and Wed
    Press โ–ถ to watch Python run this, one line at a time.
    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.
    2. The Python ideaa[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.
    3. 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]

    โ–ถ Run the example ๐ŸŽฌ Watch it come alive

    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].

    Ready for more?The full lesson adds the technical explanation, interview questions and quick revision.

    Test yourself

    5 questions. Get 4 right and this lesson is marked complete.

    1. 1. a = np.array([[1, 2], [3, 4]]). What is a[1, 0]?
    2. 2. What does a[:, 1] select?
    3. 3. a = np.array([5, 6, 7, 8]). What is a[-2:]?
    4. 4. row = a[0]; row[0] = 99. What happens to a?
    5. 5. How do you make an independent copy of a row?

    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] then week[0] = 99 changes sales too. 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