list to array in numpy in python

To convert a list into a numpy array in Python, you just need to use the array() function from the numpy module.

Here is an example:

main.py
import numpy as np

my_list = [1, 2, 3, 4, 5]

my_array = np.array(my_list)

print(my_array)
93 chars
8 lines

Output:

main.py
[1 2 3 4 5]
12 chars
2 lines

In this example, we first import the numpy module and then define a simple list called my_list. We then use the array() function to create a numpy array from the list and assign it to a new variable called my_array. Finally, we print out the contents of the my_array variable to verify that the array was created correctly.

Note that the elements in the list must be of the same type for the conversion to work correctly.

gistlibby LogSnag