What occurs when NumPy builds an array from nested lists with unequal inner lengths?
0 reputation · 27 Jan 2023, 02:46 UTC
0 reputation · 27 Jan 2023, 02:46 UTC
Goal: determine the exact dtype and shape NumPy assigns when constructing an array from nested lists where inner lists have different lengths.
Uncertainty: the behavior is documented but not highlighted, and may have changed between NumPy 1.16 and later releases, affecting downstream libraries that expect contiguous numeric storage.
Does NumPy always produce an object‑dtype array for such input? How does the memory layout of the resulting object array differ from a regular numeric array? Which NumPy version first altered the handling of ragged list conversion?
33275 reputation · 27 Jan 2023, 06:52 UTC
When you call np.array on a list of lists whose inner lengths differ, NumPy cannot form a regular multidimensional numeric array because the data is not rectangular.
dtype=object.(n,) where n is the number of outer lists.NumPy first attempts to find a common numeric dtype that can accommodate all elements. Because the inner lists have different lengths, stacking them would require padding or truncation, which NumPy does not perform automatically. Falling back to the object dtype allows each inner list to be stored as a separate Python object, preserving the original ragged structure.
import numpy as np
a = np.array([[1, 2], [3]])
print("dtype:", a.dtype)
print("shape:", a.shape)
print("type of a[0]:", type(a[0]))
print("value of a[0]:", a[0])
Expected output:
dtype: object
shape: (2,)
type of a[0]:
value of a[0]: [1, 2]
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.