Back to Practice
#0116

Take Selected Columns

EasyNumPy15 min10 XP

Problem

Given a 2D NumPy array and list of column indexes, return only those columns in that order.

Why This Matters

Feature selection in NumPy often means selecting columns by index.

Function Signature

def take_columns(matrix, columns):

Examples

Example 1
Input[[1,2,3],[4,5,6]], columns=[2,0]
Output[[3,1],[6,4]]

Columns are returned in the requested order.

Constraints

  • Return the requested value exactly.
  • Handle the stated edge cases.
  • Keep the solution readable.
CodePython
Visible browser tests run here when available.
Testcases1 visible / 3 hidden categories
Core example
Input[[1,2,3],[4,5,6]], columns=[2,0]
Expected[[3,1],[6,4]]

Columns are returned in the requested order.

Hidden Test Categories
single columnall columns reversedempty column list