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 variablenp
arr = np.array([1, 2, 3, 4, 5])
- creates a numpy array with the values1, 2, 3, 4, 5
and assigns it to the variablearr
list_arr = arr.tolist()
- converts the numpy array in the variablearr
to a list and assigns it to the variablelist_arr
print(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 do I use Python Numpy to read and write Excel (.xlsx) files?
- How can I check if a certain version of Python is compatible with SciPy?
- How do I use the trapz function in Python SciPy?
- How do I create a 2D array of zeros using Python and NumPy?
- How can I use Python and SciPy to read and write WAV files?
- How do I convert a Python numpy array to a list?
- How do I use the NumPy transpose function in Python?
- How do I use the scipy ttest_ind function in Python?
- How can I use Python and SciPy to implement a quantum Monte Carlo simulation?
See more codes...