What is a 0-dimensional array in NumPy, and how do you extract a Python scalar from an array using .item() or .tolist()?

Quick Overview

Asks what a 0-dimensional NumPy array is and how .item() and .tolist() turn array values into native Python scalars. Tests shape and dtype basics, the difference between arrays, NumPy scalars and Python scalars, and why the conversion matters for JSON.

What is a 0-dimensional array in NumPy, and how do you extract a Python scalar from an array using .item() or .tolist()?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

What is a 0-dimensional array in NumPy, and how do you extract a Python scalar from an array using `.item()` or `.tolist()`? ```hint Compare three kinds of "one number" Put `5`, `np.array(5)` and `np.array([1, 2, 3]).sum()` side by side and check `type`, `shape` and `ndim` where they exist. ``` ### What a Strong Answer Covers - What makes an array 0-dimensional (`ndim`, `shape`, `dtype`) and how it differs from a plain Python scalar - What `.item()` returns, with and without an index, and when it raises an error - What `.tolist()` returns for 0-d and for multi-dimensional arrays - Why native Python scalars matter downstream, for example in JSON serialization ### Follow-up Questions - How does a 0-d array differ from a NumPy scalar such as `np.float64`, and which one does `arr.sum()` return? - Why does `json.dumps` fail on `np.int64` but succeed on `np.float64`? - What do `x[()]` and `x[...]` return for a 0-d array `x`? - When would you keep values as NumPy types instead of converting them?

Overview: Asks what a 0-dimensional NumPy array is and how .item() and .tolist() turn array values into native Python scalars. Tests shape and dtype basics, the difference between arrays, NumPy scalars and Python scalars, and why the conversion matters for JSON.

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

What is a 0-dimensional array in NumPy, and how do you extract a Python scalar from an array using .item() or .tolist()?

What a Strong Answer Covers Guidance

  • What makes an array 0-dimensional ( ndim , shape , dtype ) and how it differs from a plain Python scalar
  • What .item() returns, with and without an index, and when it raises an error
  • What .tolist() returns for 0-d and for multi-dimensional arrays
  • Why native Python scalars matter downstream, for example in JSON serialization

Follow-up Questions Guidance

  • How does a 0-d array differ from a NumPy scalar such as np.float64 , and which one does arr.sum() return?
  • Why does json.dumps fail on np.int64 but succeed on np.float64 ?
  • What do x[()] and x[...] return for a 0-d array x ?
  • When would you keep values as NumPy types instead of converting them?