Numpy array to list



A common need to is to from a Python list to a numpy array and back. Let's learn how to do both.

import numpy as np

# python list to numpy array
arr = np.array([1, 2, 3, 4])

# numpy array to python list
lst = list(arr)

And that's it! To go from a list to a numpy array, we use the np.array function. To return from a numpy array back to a python list, we use the list() function.