Numpy way of appending the data retrieved from for loop
Question: I am looking for the numpy way of appending the data retrieved from for loop
as in the example below:

    
    
    import glob, gdal, numpy as np
    
    tiff_files = glob.glob('*.tif')
    
    all_data = []   #LOOKING FOR ALTERNATIVE HERE
    for f in tiff_files:
        data_open = gdal.Open(f)
        data_array = data_open.ReadAsArray().astype(np.float32)
        all_data.append(data_array)  # #LOOKING FOR ALTERNATIVE HERE
    

The above code works, but would be better to append the data_array retrieved
from for loop directly into the numpy array rather than using python list. Any
idea would be appreciated.


Answer: If you want to make it a one-liner:

    
    
    np.array([gdal.Open(f).ReadAsArray().astype(np.float32) for f in glob.glob('*.tif')])
    

but making data in a `ndarray` is not faster than the builtin `list`. E.g:

    
    
    In [391]: timeit a=[0]*1000
    100000 loops, best of 3: 10.1 us per loop
    
    In [392]: timeit a=np.array([0]*1000)
    1000 loops, best of 3: 265 us per loop
    

