Convert a nested Python list to a 2-D NumPy array of a given dtype

Quick Overview

Convert a nested Python list of numbers into a 2-D NumPy array whose dtype matches a requested type such as float32. It tests how NumPy infers shape from nesting, the difference between setting the dtype at construction and casting afterward, and what narrowing casts do to the values.

Convert a nested Python list to a 2-D NumPy array of a given dtype

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Write `to_typed_array(rows, dtype)` that converts a nested Python list of numbers, where every inner list has the same length, into a 2-D NumPy array of the requested `dtype` (for example `np.float32`). The result must be an `np.ndarray` with `.ndim == 2`, and its `.dtype` must equal the requested dtype exactly. ```python data = [[1, 2, 3], [4, 5, 6]] to_typed_array(data, np.float32) # array([[1., 2., 3.], # [4., 5., 6.]], dtype=float32) ``` ```hint When to set the type Decide whether the element type should be fixed while the array is built or applied afterward, and think about what each choice costs in memory and what it does to values the type cannot hold. ``` ### What a Strong Answer Covers - Building the array and setting the dtype in one step, with the `.ndim` and `.dtype` requirements checked - How NumPy reads the shape from the nesting, and what happens when the inner lists have different lengths - The difference between passing `dtype` to the constructor and calling `.astype` afterward - What a narrowing dtype does to the values, such as floats cast to integers or integers too large for the type - Behavior for degenerate input such as a flat list or an empty list ### Follow-up Questions - How do `np.array(data, dtype=...)` and `np.array(data).astype(...)` differ here? - What happens if one inner list is shorter than the others? - What happens when the requested dtype cannot hold the values, for example floats into `np.int32` or the integer 300 into `np.uint8`? - How would you guarantee a 2-D result when the caller passes a flat list?

Overview: Convert a nested Python list of numbers into a 2-D NumPy array whose dtype matches a requested type such as float32. It tests how NumPy infers shape from nesting, the difference between setting the dtype at construction and casting afterward, and what narrowing casts do to the values.

General logo
General
Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Write to_typed_array(rows, dtype) that converts a nested Python list of numbers, where every inner list has the same length, into a 2-D NumPy array of the requested dtype (for example np.float32).

The result must be an np.ndarray with .ndim == 2, and its .dtype must equal the requested dtype exactly.

data = [[1, 2, 3], [4, 5, 6]]

to_typed_array(data, np.float32)
# array([[1., 2., 3.],
#        [4., 5., 6.]], dtype=float32)

What a Strong Answer Covers Guidance

  • Building the array and setting the dtype in one step, with the .ndim and .dtype requirements checked
  • How NumPy reads the shape from the nesting, and what happens when the inner lists have different lengths
  • The difference between passing dtype to the constructor and calling .astype afterward
  • What a narrowing dtype does to the values, such as floats cast to integers or integers too large for the type
  • Behavior for degenerate input such as a flat list or an empty list

Follow-up Questions Guidance

  • How do np.array(data, dtype=...) and np.array(data).astype(...) differ here?
  • What happens if one inner list is shorter than the others?
  • What happens when the requested dtype cannot hold the values, for example floats into np.int32 or the integer 300 into np.uint8 ?
  • How would you guarantee a 2-D result when the caller passes a flat list?