Numpy python How to change values ​​effectively


In my program I current create a numpy array full of zeros and then for loop through each element replacing it with the desired value. Is there a more efficient way of doing this?

Below is an example of what I am doing however, instead of a int I have a list of each row which needs put into the numpy array. Is there a way to put replace whole rows and is that more efficient.

import numpy as np
from tifffile import imsave

image = np.zeros((5, 2160, 2560), 'uint16')

num =0
for pixel in np.nditer(image, op_flags=['readwrite']):
     pixel = num
     num += 1
imsave('multipage.tif', image)

Just assign to the whole row using slicing

import numpy as np
from tifffile import imsave

list_of_rows = ... # all items in list should have same length
image = np.zeros((len(list_of_rows),'uint16')

for row_idx, row in enumerate(list_of_rows):
    image[row_idx, :] = row

imsave('multipage.tif', image)

Numpy slicing is extremely powerful and nice. I recommend reading through this documentation to get a feeling of what is possible.