How can I turn a list into an array in python?

How to include a list, for example:

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

into an array (I use numpy), which looks like this:

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

Is it possible to cut segments from the beginning of the list and add them to an empty array?

thank

+5
source share
1 answer
>>> import numpy as np
>>> np.array(data_list).reshape(-1, 2)
array([[0, 1],
       [2, 3],
       [4, 5],
       [6, 7],
       [8, 9]])

(The method reshapereturns a new “view” in the array; it does not copy data.)

+17
source

All Articles