With NumPy, np.array
objects can be converted to a list with the tolist()
function. The tolist()
function doesn’t accept any arguments. If the array is one-dimensional, a list with the array elements is returned. For a multi-dimensional array, a nested list is returned.
In order to complete this tutorial, you will need:
pip
to install packages. And familiarity with coding in Python. How to Code in Python 3 series or using VS Code for Python.This tutorial was tested with Python 3.9.6 and NumPy 1.23.3.
Let’s construct a one-dimensional array of [1, 2, 3]
:
This code will output:
Now, let’s use tolist()
:
This new code will output:
The array has been converted from numpy
scalars to Python scalars.
Let’s construct a multi-dimensional array of [ [1, 2, 3], [4, 5, 6] ]
:
This code will output:
Now, let’s use tolist()
:
This new code will output:
The array has been converted from numpy
scalars to Python scalars.
When working with machine learning libraries like scikit-learn, it’s often necessary to convert NumPy arrays to lists for compatibility. For example, some algorithms in scikit-learn
require input data to be in a list format. By using tolist()
, you can ensure that your data is in the correct format for processing.
When exporting data to CSV or JSON formats, it’s often necessary to flatten multi-dimensional arrays to ensure compatibility with these formats. tolist()
can be used to flatten arrays before exporting.
tolist()
and list()
When converting NumPy arrays to lists, both tolist()
and list()
can be used. However, there are notable performance disparities between these methods, particularly when dealing with large arrays. The tolist()
method is optimized for converting NumPy arrays to lists, making it a more efficient choice for large datasets.
To illustrate the performance difference, consider the following benchmark that compares the execution times of tolist()
and list()
for converting a large NumPy array to a list:
tolist()
vs list()
When it comes to converting NumPy arrays to lists, both tolist()
and list()
can be used. However, there are significant performance differences between the two methods, especially for large arrays.
Here’s a simple benchmark to compare the performance of tolist()
and list()
:
Sample Output:
As you can see from the sample output, tolist()
is significantly faster than list()
for converting large NumPy arrays to lists.
When dealing with complex, nested arrays that contain mixed types, tolist()
can be particularly useful. It recursively converts the entire array structure to a list, preserving the nested structure and handling mixed types correctly.
Here’s an example of converting a complex, nested array with mixed types to a list:
This will output:
As you can see, the tolist()
method has successfully converted the complex, nested array with mixed types to a list, preserving the original structure and handling the mixed types correctly.
list()
on multi-dimensional arrays leading to nested listsA common mistake is using the built-in list()
function on a multi-dimensional NumPy array, which results in a nested list structure. This can lead to unexpected behavior or errors in downstream processing.
tolist()
vs flatten().tolist()
confusionAnother common confusion is between using tolist()
directly on a multi-dimensional array versus using flatten().tolist()
. While both methods can be used to flatten arrays, they have different effects on the resulting list structure.
You can convert a NumPy array to a list in Python using the tolist()
method. This method is specifically designed for this purpose and is optimized for performance. Here’s an example:
This will output: List from NumPy array: [1, 2, 3, 4, 5]
The main difference between tolist()
and list()
is that tolist()
is optimized for converting NumPy arrays to lists, while list()
is a general-purpose function that converts any iterable to a list. Here’s an example to illustrate the difference:
Both will output the same result: [1, 2, 3, 4, 5]
. However, tolist()
is more efficient for large arrays.
You can convert a 2D NumPy array to a list of lists using the tolist()
method. This method will convert the 2D array to a nested list structure, where each sub-array is converted to a list. Here’s an example:
This will output: List of lists from 2D NumPy array: [[1, 2, 3], [4, 5, 6]]
Yes, you can flatten a NumPy array and convert it to a list using the flatten()
method. This method will return a new array with the same elements as the original array, but with a one-dimensional structure. Here’s an example:
This will output: Flattened list from 2D NumPy array: [1, 2, 3, 4, 5, 6]
In this tutorial, you learned how to use tolist()
to convert np.array
objects to lists. It is applicable to one-dimensional and multi-dimensional arrays. We also covered the process of converting a 2D NumPy array to a list of lists and flattening a NumPy array to a list.
If you’re interested in learning more about NumPy, check out the following tutorials:
Continue building with DigitalOcean Gen AI Platform.
Thank you for this i can convert ndarray to list obj
- arnaldo
thanks for your help man , great buddy . actualluy i was working in opencv2 and the value was in array and i cant turn it until i found that it was a numpy array . hahahahahah
- akshit