Convert NumPy Array to List in Python Easily

Introduction
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:
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 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 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 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 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.
Data conversion for machine learning libraries (e.g., scikit-learn)
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 data_list = data.tolist() X_train, X_test, y_train, y_test = train_test_split(data_list, target, test_size=0.2, random_state=42)
Flattening arrays before exporting to CSV/JSON
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 arr_2d = np.array([[1, 2, 3], [4, 5, 6]]) flattened_list = arr_2d.tolist() df = pd.DataFrame(flattened_list) df.to_csv(‘exported_data.csv’, index=False)
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:
Performance benchmarks: 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 large_array = np.random.rand(10000, 10000) start_time = time.time() tolist_result = large_array.tolist() tolist_time = time.time() – start_time print(f”tolist() conversion time: {tolist_time} seconds”) 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 complex_array = np.array([1, ‘a’, [2, ‘b’, [3, ‘c’]]]) 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.
Using list() on multi-dimensional arrays leading to nested lists
A 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.
arr_2d = np.array([[1, 2, 3], [4, 5, 6]]) nested_list = list(arr_2d) arr_2d = np.array([[1, 2, 3], [4, 5, 6]]) flattened_list = arr_2d.tolist()
tolist() vs flatten().tolist() confusion
Another 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.
arr_2d = np.array([[1, 2, 3], [4, 5, 6]]) flattened_list = arr_2d.tolist() arr_2d = np.array([[1, 2, 3], [4, 5, 6]]) flattened_list = arr_2d.flatten().tolist()
1. How do I convert a NumPy array to a list in Python?
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 arr = np.array([1, 2, 3, 4, 5]) 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]
2. What is the difference between tolist() and list()?
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 arr = np.array([1, 2, 3, 4, 5]) list_from_arr_tolist = arr.tolist() 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.
3. How do I convert a 2D NumPy array to a list of lists?
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 arr_2d = np.array([[1, 2, 3], [4, 5, 6]]) 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]]
4. Can I flatten a NumPy array and convert it to a list?
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 arr_2d = np.array([[1, 2, 3], [4, 5, 6]]) 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: