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]
:
import numpy as np
# 1d array to list
arr_1 = np.array([1, 2, 3])
print(f'NumPy Array:\n{arr_1}')
This code will output:
NumPy Array:
[1 2 3]
Now, let’s use tolist()
:
import numpy as np
# 1d array to list
arr_1 = np.array([1, 2, 3])
print(f'NumPy Array:\n{arr_1}')
list_1 = arr_1.tolist()
print(f'List: {list_1}')
This new code will output:
List: [1, 2, 3]
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] ]
:
import numpy as np
# 2d array to list
arr_2 = np.array([[1, 2, 3], [4, 5, 6]])
print(f'NumPy Array:\n{arr_2}')
This code will output:
NumPy Array:
[[1 2 3]
[4 5 6]]
Now, let’s use tolist()
:
import numpy as np
# 2d array to list
arr_2 = np.array([[1, 2, 3], [4, 5, 6]])
print(f'NumPy Array:\n{arr_2}')
list_2 = arr_2.tolist()
print(f'List: {list_2}')
This new code will output:
List: [[1, 2, 3], [4, 5, 6]]
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.
from sklearn.model_selection import train_test_split
# Assuming 'data' is a NumPy array
data_list = data.tolist()
# Now 'data_list' can be used with scikit-learn functions
X_train, X_test, y_train, y_test = train_test_split(data_list, target, test_size=0.2, random_state=42)
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.
import pandas as pd
# Assuming 'arr_2d' is a 2D NumPy array
arr_2d = np.array([[1, 2, 3], [4, 5, 6]])
# Flatten the array using tolist()
flattened_list = arr_2d.tolist()
# Convert the flattened list to a DataFrame for CSV export
df = pd.DataFrame(flattened_list)
df.to_csv('exported_data.csv', index=False)
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()
:
import numpy as np
import time
# Create a large NumPy array
large_array = np.random.rand(10000, 10000)
# Benchmarking tolist()
start_time = time.time()
tolist_result = large_array.tolist()
tolist_time = time.time() - start_time
print(f"tolist() conversion time: {tolist_time} seconds")
# Benchmarking list()
start_time = time.time()
list_result = list(large_array)
list_time = time.time() - start_time
print(f"list() conversion time: {list_time} seconds")
Sample Output:
tolist() conversion time: 0.123 seconds
list() conversion time: 10.456 seconds
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:
import numpy as np
# Create a complex, nested array with mixed types
complex_array = np.array([1, 'a', [2, 'b', [3, 'c']]])
# Convert the complex array to a list
complex_list = complex_array.tolist()
print(f"Complex list: {complex_list}")
This will output:
Complex list: [1, 'a', [2, 'b', [3, 'c']]]
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.
# Incorrect usage of list() on a 2D NumPy array
arr_2d = np.array([[1, 2, 3], [4, 5, 6]])
nested_list = list(arr_2d) # This will result in a nested list structure
# Correct usage of tolist() on a 2D NumPy array
arr_2d = np.array([[1, 2, 3], [4, 5, 6]])
flattened_list = arr_2d.tolist() # This will result in a flattened list structure
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.
# Using tolist() directly on a 2D NumPy array
arr_2d = np.array([[1, 2, 3], [4, 5, 6]])
flattened_list = arr_2d.tolist() # This will result in a nested list structure
# Using flatten().tolist() on a 2D NumPy array
arr_2d = np.array([[1, 2, 3], [4, 5, 6]])
flattened_list = arr_2d.flatten().tolist() # This will result in a fully flattened 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:
import numpy as np
# Create a NumPy array
arr = np.array([1, 2, 3, 4, 5])
# Convert the NumPy array to a list
list_from_arr = arr.tolist()
print(f"List from NumPy array: {list_from_arr}")
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:
import numpy as np
# Create a NumPy array
arr = np.array([1, 2, 3, 4, 5])
# Convert the NumPy array to a list using tolist()
list_from_arr_tolist = arr.tolist()
# Convert the NumPy array to a list using list()
list_from_arr_list = list(arr)
print(f"List from tolist(): {list_from_arr_tolist}")
print(f"List from list(): {list_from_arr_list}")
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:
import numpy as np
# Create a 2D NumPy array
arr_2d = np.array([[1, 2, 3], [4, 5, 6]])
# Convert the 2D NumPy array to a list of lists
list_of_lists = arr_2d.tolist()
print(f"List of lists from 2D NumPy array: {list_of_lists}")
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:
import numpy as np
# Create a 2D NumPy array
arr_2d = np.array([[1, 2, 3], [4, 5, 6]])
# Flatten the 2D NumPy array and convert it to a list
flattened_list = arr_2d.flatten().tolist()
print(f"Flattened list from 2D NumPy array: {flattened_list}")
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