array to list in python

To convert an array to a list in Python, you can use the tolist() method of the NumPy array or simply cast it to a Python list using the list() function.

Here's an example using NumPy:

main.py
import numpy as np

arr = np.array([1, 2, 3, 4, 5])

lst = arr.tolist()

print(lst) # [1, 2, 3, 4, 5]
102 chars
8 lines

And here's an example using the list() function:

main.py
arr = [1, 2, 3, 4, 5]

lst = list(arr)

print(lst) # [1, 2, 3, 4, 5]
69 chars
6 lines

Both methods will give you a Python list that you can use in your program.

related categories

gistlibby LogSnag