There are a few ways of converting a numpy array to a python list. The numpy ndarray object has a handy tolist[] function that you can use to convert the respect numpy array to a list. You can also use the Python built-in list[] function to get a list from a numpy array. Lets see their usage through some examples.
1. Using numpy ndarray tolist[] function
It returns a copy of the array data as a Python list. The list maybe nested depending on the dimensionality of the numpy array. The following is the syntax:
# arr is a numpy array ls = arr.tolist[]Note that the tolist[] function does not take any arguments. Also, in the list returned, the data items do not retain their numpy data types, they are converted to their nearest compatible built-in Python types.
Lets look at some the examples of using the numpy ndarray tolist[] function.
1.1 Convert a 1D numpy array to a list
import numpy as np # sample numpy 1D array arr = np.array[[1,2,3,4]] # print print["Numpy array: ", arr] print["Type: ",type[arr]] # convert to a list ls = arr.tolist[] # print print["\nList: ", ls] print["Type: ",type[ls]]Output:
Numpy array: [1 2 3 4] Type: List: [1, 2, 3, 4] Type:In the above example, the tolist[] function is applied to the numpy array arr and the returned list is saved to ls.
1.2 Convert a 2D numpy array to a list
import numpy as np # sample numpy 2D array arr = np.array[[[1,2,3],[4,5,6]]] # print print["Numpy array: ", arr] print["Type: ",type[arr]] # convert to a list ls = arr.tolist[] # print print["\nList: ", ls] print["Type: ",type[ls]]Output:
Numpy array: [[1 2 3] [4 5 6]] Type: List: [[1, 2, 3], [4, 5, 6]] Type:Note that the returned list is nested because the numpy array was multi-dimensional.
2. Using the built-in list[] function
You can also use the built-in Python function list[] to convert a numpy array. The following is the syntax:
# arr is a numpy array ls = list[arr]Lets look at some the examples of using the list[] function.
2.1 Convert a 1D array to a list
import numpy as np # sample numpy 1D array arr = np.array[[1,2,3,4]] # print print["Numpy array: ", arr] print["Type: ",type[arr]] # convert to a list using list[] ls = list[arr] # print print["\nList: ", ls] print["Type: ",type[ls]]Output:
Numpy array: [1 2 3 4] Type: List: [1, 2, 3, 4] Type:In the above example, the list[] function is applied on the numpy array arr and the returned list is saved to ls.
2.2 Convert a 2D array to a list
import numpy as np # sample numpy 2D array arr = np.array[[[1,2,3],[4,5,6]]] # print print["Numpy array: ", arr] print["Type: ",type[arr]] # convert to a list using list[] ls = list[arr] # print print["\nList: ", ls] print["Type: ",type[ls]]Output:
Numpy array: [[1 2 3] [4 5 6]] Type: List: [array[[1, 2, 3]], array[[4, 5, 6]]] Type:Using the list[] function on multi-dimensional array returns a list of numpy arrays.
print[type[ls[0]]]Output:
3. Difference between numpy ndarray tolist[] and the built-in list[] functions
The above examples illustrated the usage of the two functions. The returned lists were different when these functions were applied to a multi-dimensional array. There is, however, one more major difference between the two. With the numpy ndarray tolist[] function, the data items are converted to their nearest compatible built-in Python types whereas, with the list[] function, the numpy types of the data items are preserved. See the example below:
import numpy as np # sample numpy 1D array arr = np.array[[1,2,3,4]] # convert to a list using tolist[] ls1 = arr.tolist[] # convert to a list using list[] ls2 = list[arr] # print ls1 print["ls1: ", ls1] print["Type of items: ",[type[i] for i in ls1]] # print ls2 print["\nls2: ", ls2] print["Type of items: ",[type[i] for i in ls2]]Output:
ls1: [1, 2, 3, 4] Type of items: [, , , ] ls2: [1, 2, 3, 4] Type of items: [, , , ]You can see that in the list returned by the tolist[] function, ls1, each item has been converted to its compatible python data type whereas, in the list returned by the list[] function, ls2, each item retains its type from the numpy array.
4. Conclusion
Generally, when converting a numpy array to a Python list, use the numpy ndarray tolist[] function. It returns a list with python compatible types and works well with multidimensional arrays. Use the list[] function when you want to retain the numpy types.
For more on the numpy ndarray tolist[] function, refer to its offical documentation.
With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in aJupyter Notebookwith a python [version 3.8.3] kernel having numpy version 1.18.5
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.
Tutorials on python lists:
- Python Check if an element is in a list
- Python Iterate over multiple lists in parallel using zip[]
- Python Flatten a list of lists to a single list
- Pandas DataFrame to a List in Python
- Python Convert List to a String
- Convert Numpy array to a List With Examples
- Python List Comprehension With Examples
- Python List Index With Examples
- Python List Count Item Frequency
- Python List Length
- Python Sort a list With Examples
- Python Reverse a List With Examples
- Python Remove Duplicates from a List
- Python list append, extend and insert functions.
- Python list remove, pop and clear functions.