python-scipyHow do I convert a Python numpy.ndarray to a list?
To convert a Python numpy.ndarray to a list, you can use the tolist() method. This method will return a copy of the array data as a list.
For example:
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
list_arr = arr.tolist()
print(list_arr)
Output example
[1, 2, 3, 4, 5]
The code above has the following parts:
import numpy as np- imports the numpy library and assigns it to the variablenparr = np.array([1, 2, 3, 4, 5])- creates a numpy array with the values1, 2, 3, 4, 5and assigns it to the variablearrlist_arr = arr.tolist()- converts the numpy array in the variablearrto a list and assigns it to the variablelist_arrprint(list_arr)- prints the list in the variablelist_arr
Helpful links
More of Python Scipy
- How do I use Python XlsxWriter to write a NumPy array to an Excel file?
- How can I use Python Scipy to zoom in on an image?
- How do I upgrade my Python Scipy package?
- How do I create a 2D array of zeros using Python and NumPy?
- How can I use Python and Numpy to parse XML data?
- How can I use Python and SciPy to apply a Hann window to a signal?
- How do I create a zero matrix using Python and Numpy?
- How do I create a numpy array of zeros using Python?
- How do I create a numpy array of zeros using Python?
- How can I use Python and SciPy to find the zeros of a function?
See more codes...