Shan Shan - 1 year ago 214
Python Question

how to convert 2d list to 2d numpy array?

I have a 2D list something like

a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

and I want to convert it to a 2d numpy array. Can we do it without allocating memory like


and then storing values to it?

Answer Source

Just pass the list to np.array:

a = np.array(a)

You can also take this opportunity to set the dtype if the default is not what you desire.

a = np.array(a, dtype=...)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download