To convert a NumPy array to a Python list, you can use the tolist()
method of the NumPy array. Here’s how you can do it:
import numpy as np
# Create a NumPy array
numpy_array = np.array([1, 2, 3, 4, 5])
# Convert the NumPy array to a Python list
python_list = numpy_array.tolist()
# Print the Python list
print(python_list)
In this example, numpy_array
is converted to a Python list using the tolist()
method, and the result is stored in the variable python_list
. When you print python_list
, you’ll see the NumPy array’s elements as a Python list.