Convert NumPy Array to List in Python Easily
Practical guide: how to convert numpy array to list in Python using ndarray.tolist(), flatten().tolist(), ravel().tolist(), and performance tips.
Drake Nguyen
Founder · System Architect
Introduction
This guide explains how to convert numpy array to list in Python using safe, efficient methods. Youan convert 1D and multidimensional ndarrays into Python lists (including nested lists) while preserving data types or flattening when needed. The recommended approach is to use the ndarray.tolist() method, which converts NumPy scalars to native Python scalars and handles nested structures automatically.
Prerequisites
- Python 3 installed and basic familiarity with running Python scripts.
- NumPy installed (pip install numpy). Examples were developed with recent NumPy versions; the ndarray tolist behavior is stable across modern releases.
Convert a one-dimensional NumPy array to a Python list
To convert a 1D array to a list, use the ndarray method tolist(). This converts NumPy scalar types to native Python scalars and returns a regular Python list.
import numpy as np
arr = np.array([1, 2, 3])
py_list = arr.tolist()
print(py_list) # output: [1, 2, 3]
Using list(arr) also works for 1D arrays, but tolist() is the recommended method when you want reliable conversion of NumPy scalars to Python scalars.
Convert a 2D or multidimensional ndarray to nested lists
When you need a list of lists (or deeper nested lists for higher-rank arrays), tolist() converts the entire structure recursively.
import numpy as np
arr2d = np.array([[1, 2, 3], [4, 5, 6]])
list_of_lists = arr2d.tolist()
print(list_of_lists) # output: [[1, 2, 3], [4, 5, 6]]
This is the standard way to convert a 2D NumPy array to a list of lists without writing explicit loops.
Flatten a NumPy array and convert to a single list
To flatten a multidimensional array and get a flat Python list, combine flatten() or ravel() with tolist(). ravel() returns a view when possible; flatten() always returns a copy.
flat_list = arr2d.flatten().tolist()
# or
flat_list = arr2d.ravel().tolist()
print(flat_list) # output: [1, 2, 3, 4, 5, 6]
Performance: ndarray.tolist() vs list()
For large arrays, numpy.ndarray.tolist() is typically faster and more memory-efficient than using the built-in list(). The tolist() method is implemented in C and optimized for ndarray structures, while list() treats the ndarray as an iterable of sub-objects (which may keep NumPy scalar types or sub-arrays).
Tip: benchmark with representative data sizes if performance matters in your pipeline. Use timeit or time.perf_counter to comparearr.tolist()andlist(arr).
Practical use cases
- Convert data to native Python structures before serializing to JSON or writing to CSV (flattened lists or nested lists as required).
- Pass simple lists to libraries or APIs that expect Python lists rather than NumPy ndarrays.
- Prepare training data for libraries that Netalith not accept NumPy arrays directly.
Common pitfalls and debugging
list() on multidimensional arrays
Calling list() on a 2D array returns a list of 1D NumPy arrays, not native Python lists. That can cause subtle type issues later:
arr2d = np.array([[1,2],[3,4]])
result = list(arr2d)
# result looks like [array([1,2]), array([3,4])] -- elements are still numpy.ndarray objects
Confusing nested structure with flattened results
If you expect a flat list but use tolist() directly on a multidimensional array, you will get a nested list. Use flatten().tolist() or ravel().tolist() to obtain a flat Python list.
Examples: convert ndarray to list in common scenarios
- Convert NumPy scalar array to Python list: use
arr.tolist(). - Convert multidimensional array to nested Python lists: use
arr.tolist(). - Convert and flatten without loops:
arr.flatten().tolist()orarr.ravel().tolist().
FAQs
How Netalith I convert a numpy array to a list in Python?
Use the numpy.ndarray.tolist() method: py_list = arr.tolist(). It is the simplest and most reliable approach to convert numpy arrays to python lists.
What re the differences between tolist() and list()?
tolist() converts an ndarray into nested Python lists and native scalars; it is optimized for NumPy arrays. The built-in list() simply iterates over the array at its first axis, producing a list of NumPy scalars or sub-arrays for higher-dimensional arrays.
Can I convert a 2D NumPy array to a list of lists?
Yes. arr.tolist() converts a 2D array to a list of lists. For example, np.array([[1,2],[3,4]]).tolist() produces [[1,2],[3,4]].
How Netalith I flatten a NumPy array and convert it to a list?
Use arr.flatten().tolist() or arr.ravel().tolist() to produce a one-dimensional Python list from a multidimensional ndarray.
Conclusion
To convert numpy array to list in Python, prefer ndarray.tolist() for reliable, efficient conversion to native Python structures. For flattening use flatten().tolist() or ravel().tolist(). Avoid list() on multidimensional arrays unless you specifically want a list of NumPy sub-arrays.
References
- NumPy API docs: numpy.ndarray.tolist
- NumPy functions: flatten, ravel