easy +10 pts

Fancy Index Select

Select rows from a matrix using a list of indices and return a new 2D array.

Write a function `fancy_index_select(matrix, indices)` that takes a 2D list `matrix` (list of lists of numbers) and a list of integers `indices`. The function should return a new 2D list containing the rows of `matrix` at the given indices, in the order they appear in `indices`. Duplicate indices are allowed. Do not modify the original matrix. Return the resulting list of rows.

Constraints

`matrix` is a non-empty 2D list with at least one row and one column; all rows have the same length. `indices` is a list of integers, each satisfying `0 <= idx < len(matrix)`. The output has `len(indices)` rows, each of length `len(matrix[0])`.

Example

>>> fancy_index_select([[1, 2], [3, 4], [5, 6]], [0, 2])
[[1, 2], [5, 6]]
>>> fancy_index_select([[1, 2], [3, 4], [5, 6]], [2, 0, 1])
[[5, 6], [1, 2], [3, 4]]
10 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Use a list comprehension to build the result row by row.
Iterate over `indices` and access `matrix[i]` for each `i`.
Return a new list; avoid mutating the original matrix.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.