python-scipyHow do I convert a Python Numpy array to a list?
The easiest way to convert a Python Numpy array to a list is to use the tolist()
method. This method will convert the Numpy array into a list, and return the list.
For example, let's create a Numpy array:
import numpy as np
arr = np.array([1,2,3,4])
Now, let's convert it to a list:
list_arr = arr.tolist()
print(list_arr)
Output example
[1, 2, 3, 4]
The tolist()
method is a built-in method of the Numpy library, and it takes no arguments.
Additionally, if you just want to iterate over the elements of the Numpy array, you can use the for
loop syntax, as follows:
for elem in arr:
print(elem)
Output example
1
2
3
4
Helpful links
More of Python Scipy
- How can I check if a certain version of Python is compatible with SciPy?
- How can I use Python Numpy to select elements from an array based on multiple conditions?
- How do I use the scipy ttest_ind function in Python?
- How do I use the NumPy transpose function in Python?
- How do I create a 2D array of zeros using Python and NumPy?
- How do I use Python Numpy to read and write Excel (.xlsx) files?
- How do I use Python and SciPy to write a WAV file?
- How can I use Python and SciPy to generate a uniform distribution?
- How do I use Python and SciPy to create a tutorial PDF?
- How can I use Python and SciPy to perform a Short-Time Fourier Transform?
See more codes...