v9.1.1
Menu

2D Array Indexing

easyPROSubsetting, Slicing, IndexingNumPy IndexingNumpy

Problem Statement

Access elements and slices from a 2D NumPy array.

Background

For 2D arrays:

  • arr[i, j] - element at row i, column j
  • arr[i] or arr[i, :] - entire row i
  • arr[:, j] - entire column j

Your Task

Write a function index_2d(arr) that returns a dictionary with:

  • "element_0_0": Element at position (0, 0), as a plain Python int
  • "element_1_2": Element at position (1, 2), as a plain Python int
  • "row_0": First row as a Python list
  • "col_1": Second column as a Python list

Output Format

Return a dictionary with exactly these four keys.

Example

Input
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Output
{'element_0_0': 1, 'element_1_2': 6, 'row_0': [1, 2, 3], 'col_1': [2, 5, 8]}
Reasoning
  • arr is a 3×33 \times 3 grid of integers. Positions count from zero, so position (i,j)(i, j) means row ii, column jj, and the top-left number sits at (0,0)(0, 0).
  • element_0_0: row 0, column 0 holds a00=1a_{00} = 1.
  • element_1_2: row 1 is [4,5,6][4, 5, 6], and its column 2 (the third entry, not the second) is a12=6a_{12} = 6.
  • row_0: fix i=0i = 0 and let jj run over every column: a00,a01,a02a_{00}, a_{01}, a_{02} gives [1, 2, 3].
  • col_1: fix j=1j = 1 and let ii run over every row: a01,a11,a21a_{01}, a_{11}, a_{21} gives [2, 5, 8], the middle number of each row read top to bottom.
  • The two elements are single plain integers; the row and column are ordinary lists. Notice a row's length equals the number of columns and a column's length equals the number of rows: for a 2×42 \times 4 input, row_0 has 4 entries but col_1 has only 2.

Constraints

  • Input will be a 2D array with at least 2 rows and 3 columns
  • Use arr[i, j] syntax for elements

More from Numpy

PRO

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test results

Sign in to run your code against the test cases.