There are many ways to create a NumPy array . Passing a Python list to np.array or np.asarray is one such way.
Another way is to use an iterator:
In [11]: np.fromiter(xrange(10), count=10, dtype='float') Out[11]: array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])
In this case, there is no large Python temporary list. Therefore, instead of creating a Python list, you can define a generator function that gives the elements in the list. Then, to create an array, you must pass the generator to np.fromiter . Since np.fromiter always creates a 1D array, use reshape for the return value to create arrays with a higher dimension.
There is also np.fromfunction , np.frombuffer , np.fromfile , np.loadtxt , np.genfromtxt , np.fromstring , np.zeros , np.empty and np.ones . All of this provides ways to create NumPy arrays without creating large Python temporary objects.
source share