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 using the tolist()
function. This function will convert the array to a list and return it.
Example
import numpy as np
a = np.array([[1,2,3], [4,5,6]])
list_a = a.tolist()
print(list_a)
Output example
[[1, 2, 3], [4, 5, 6]]
The tolist()
function takes the array and converts it into a list of the same elements. The elements are grouped by their original position in the array.
Code explanation
- Importing the numpy library as np:
import numpy as np
- Creating the array:
a = np.array([[1,2,3], [4,5,6]])
- Converting the array to a list:
list_a = a.tolist()
- Printing the list:
print(list_a)
Helpful links
More of Python Scipy
- How can I use Python Scipy to zoom in on an image?
- How do I use Python Scipy to perform a Z test?
- How can I check if a certain version of Python is compatible with SciPy?
- How do I create a 2D array of zeros using Python and NumPy?
- How to use Python, XML-RPC, and NumPy together?
- How can I use Python and SciPy to find the zeros of a function?
- How can I use Python and Numpy to zip files?
- How do I use Python XlsxWriter to write a NumPy array to an Excel file?
- How can I use Python and SciPy to visualize data?
- How do I use Python and SciPy to write a WAV file?
See more codes...