Table of Contents
Converting a NumPy array to a list in Python involves using the built-in function “tolist()” which allows for the transformation of a NumPy array into a standard Python list. This process can be useful for accessing individual elements, manipulating data, or performing operations that are not supported by NumPy arrays. By converting the array to a list, the data becomes more versatile and can be utilized in a wider range of applications. Overall, the conversion from a NumPy array to a list provides greater flexibility and functionality in working with data in Python.
Convert NumPy Array to List in Python (With Examples)
You can use the following basic syntax to convert a NumPy array to a list in Python:
my_list = my_array.tolist()
The following examples show how to use this syntax in practice.
Example 1: Convert 1-Dimensional Array to List
The following code shows how to convert a 1-dimensional NumPy array to a list in Python:
import numpy as np #create NumPy array my_array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) #convert NumPy array to list my_list = my_array.tolist() #view list print(my_list) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] #view object type type(my_list) list
Example 2: Convert Multi-Dimensional Array to List
The following code shows how to convert a multi-dimensional NumPy array to a list in Python:
import numpy as np #create NumPy array my_array = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]) #convert NumPy array to list my_list = my_array.tolist() #view list print(my_list) [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]] #view object type type(my_list) list
Example 3: Convert Multi-Dimensional Array to Flattened List
The following code shows how to convert a multi-dimensional NumPy array to a flattened list in Python:
import numpy as np #create NumPy array my_array = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]) #convert NumPy array to flattened list my_list = my_array.flatten().tolist() #view list print(my_list) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] #view object type type(my_list) list
The following tutorials explain how to perform other common conversions in Python:
Cite this article
stats writer (2024). How can I convert a NumPy array to a list in Python?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-convert-a-numpy-array-to-a-list-in-python/
stats writer. "How can I convert a NumPy array to a list in Python?." PSYCHOLOGICAL SCALES, 11 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-convert-a-numpy-array-to-a-list-in-python/.
stats writer. "How can I convert a NumPy array to a list in Python?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-convert-a-numpy-array-to-a-list-in-python/.
stats writer (2024) 'How can I convert a NumPy array to a list in Python?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-convert-a-numpy-array-to-a-list-in-python/.
[1] stats writer, "How can I convert a NumPy array to a list in Python?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.
stats writer. How can I convert a NumPy array to a list in Python?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
