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 do I create a 2D array of zeros using Python and NumPy?
- 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 Scipy zeros in Python?
- How do I use Python Numpy to read and write Excel (.xlsx) files?
- How can I use Python Scipy to zoom in on an image?
- How do I calculate the cross-correlation of two arrays using Python and NumPy?
- How do I create a zero matrix using Python and Numpy?
- How do I use Python Scipy to perform a Z test?
- How do I use Python and SciPy to create a tutorial PDF?
See more codes...