What is the difference between indexing an array with arr[i, j] versus arr[i][j] in NumPy?

Quick Overview

Asks how NumPy evaluates arr[i, j] compared with the chained arr[i][j]. Tests whether you know when the two forms agree, how they behave with slices and integer-array indexing, why one is faster, and what happens when you assign through a chained index.

What is the difference between indexing an array with arr[i, j] versus arr[i][j] in NumPy?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

What is the difference between indexing an array with `arr[i, j]` versus `arr[i][j]` in NumPy? ```hint Count the indexing calls Write out what each form passes to the array's `__getitem__`, then try both forms with a slice such as `:` in the first position and with lists of integers as `i` and `j`. ``` ### What a Strong Answer Covers - How each form is evaluated: one tuple index or two chained index operations - When the two forms return the same thing, and the cost difference for scalar access - How they diverge with slices and with integer-array indexing, shown on a small array - What happens when you assign through the chained form ### Follow-up Questions - Why does `arr[[0, 2]][:, 1] = -1` leave `arr` unchanged while `arr[1][2] = 99` changes it? - How would you select the block formed by rows 0 and 2 and columns 1 and 3? - What does `arr[..., 0]` select on a 3-D array? - How does chained indexing in pandas, such as `df["col"][row] = value`, relate to this?

Overview: Asks how NumPy evaluates arr[i, j] compared with the chained arr[i][j]. Tests whether you know when the two forms agree, how they behave with slices and integer-array indexing, why one is faster, and what happens when you assign through a chained index.

General logo
General
Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

What is the difference between indexing an array with arr[i, j] versus arr[i][j] in NumPy?

What a Strong Answer Covers Guidance

  • How each form is evaluated: one tuple index or two chained index operations
  • When the two forms return the same thing, and the cost difference for scalar access
  • How they diverge with slices and with integer-array indexing, shown on a small array
  • What happens when you assign through the chained form

Follow-up Questions Guidance

  • Why does arr[[0, 2]][:, 1] = -1 leave arr unchanged while arr[1][2] = 99 changes it?
  • How would you select the block formed by rows 0 and 2 and columns 1 and 3?
  • What does arr[..., 0] select on a 3-D array?
  • How does chained indexing in pandas, such as df["col"][row] = value , relate to this?