List of lists into numpy array
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Converting a Python list of lists into a NumPy array is done with np.array(). When all inner lists have the same length, the result is a regular 2D array. When they differ (ragged lists), NumPy creates an array of objects instead, which loses the performance benefits of a contiguous numeric array. Understanding this distinction is key to avoiding subtle bugs when converting nested Python data structures to NumPy.
Basic Conversion
NumPy automatically infers the shape and dtype from the input data.
Specifying dtype
Ragged (Uneven) Lists
When inner lists have different lengths, np.array() does not create a 2D numeric array:
To convert ragged lists to a regular array, pad the shorter lists:
3D and Higher Dimensions
Nested lists can create higher-dimensional arrays:
Performance: np.array vs np.fromiter
For very large datasets, consider np.vstack, np.stack, or pre-allocating the array:
Common Conversions
Common Pitfalls
- Ragged lists silently create object arrays: Before NumPy 1.24,
np.array([[1,2], [3]])silently created a 1D array of list objects. In 1.24+, it raisesVisibleDeprecationWarning. Always ensure inner lists have the same length for numeric arrays. - Mixed types causing dtype promotion: If one element is a string (
[[1, 2], [3, "four"]]), the entire array becomes dtypeobjector<U21, losing numeric operations. Validate input data types before conversion. - Assuming
np.arrayalways makes a copy:np.array(existing_array)makes a copy by default, butnp.asarray(existing_array)does not. Usenp.asarray()when you want to avoid unnecessary copies. - Memory usage with large lists: Converting a 1-million-row list of lists to an array temporarily doubles memory (the list and the array both exist). For large data, read directly into NumPy with
np.loadtxt,np.genfromtxt, orpandas.read_csv().to_numpy(). - Column-major vs row-major ordering:
np.array(data, order='C')creates row-major (C-contiguous) arrays (default).order='F'creates column-major (Fortran-contiguous). The wrong order can significantly slow operations that iterate along the non-contiguous axis.
Summary
- Use
np.array(list_of_lists)for direct conversion — works when all inner lists have equal length - Ragged (unequal-length) lists produce object arrays, not numeric arrays — pad or truncate first
- Specify
dtypeexplicitly to control the output type and avoid silent type promotion - For large data, use
np.vstack, pre-allocated arrays, ornp.fromiterfor better performance - Use
np.asarray()instead ofnp.array()to avoid unnecessary copies of existing arrays - Check
.shapeand.dtypeafter conversion to verify the result is what you expect
Related reading

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.