You could preallocate the array before assigning the respective values:

a = np.empty(shape=(25, 2), dtype=int)
for x in range(1, 6):
    for y in range(1, 6):
        index = (x-1)*5+(y-1)
        a[index] = x, y
Answer from aasoo on Stack Overflow
🌐
Tudelft
nsweb.tn.tudelft.nl › ~gsteele › TN2513_Tips › Creating a numpy array in a loop.html
Creating a numpy array in a loop
y is: <class 'numpy.ndarray'> [ 0. 1. 4. 9. 16. 25. 36. 49. 64. 81.] This is the most computationally efficient way, but it only works if you know in advance how big the final array will be! If you don't know how big it will be in then end, the first example with lists is probably your best bet. ... N = 10 # Pre-allocate an array of the right size y = np.zeros(N) # Now fill the arry for i in range(N): x = i y[i] = x**2 print("y is:") print(type(y)) print(y)
Discussions

python - Construct an numpy array inside a for loop - Stack Overflow
In order to do this you would either need to use a datatype that's size can be mutated as I did in my other answer or you could also figure out how many images you have before defining the array. (As suggested by @P.Camilleri) ... # Count nuber of images idx_count = 0 for item in dataset: idx_count += len(item['data']) # Create an empty numpy ... More on stackoverflow.com
🌐 stackoverflow.com
python - creating numpy arrays in a for loop - Stack Overflow
I would like to be able to create numpy arrays based on a number that could change. For example, say I have 50 text files containing a 2x2 set of numbers I would like to load those 50 files as numpy More on stackoverflow.com
🌐 stackoverflow.com
python - how create numpy array with for loop - Stack Overflow
Hello sorry for my english i'm not native and not good so i have some question I am creating a function in python that multiplies a desired number by a whole consecutive integer. the heart of my pr... More on stackoverflow.com
🌐 stackoverflow.com
python - Most efficient and most pythonic way to create a NumPy array within a loop - Stack Overflow
I'm currently trying to figure out the most efficient way to create a numpy array in a loop, here are the examples: import numpy as np from time import time tic = time() my_list = range(1000000) a... More on stackoverflow.com
🌐 stackoverflow.com
July 10, 2017
🌐
Akuederle
akuederle.com › quicktips › create-numpy-array-with-for-loop
The proper way to create a numpy array inside a for-loop
April 14, 2015 - If you came across the same issue, I described above, consider using a simple python list, and converting it to a numpy array at the end (this is way faster!). If you know the exact size of the final array (which I assumed you do not), you can also try initializing an empty array with this size first and then replace certain parts by index inside of the loop.
🌐
EDUCBA
educba.com › home › software development › software development tutorials › numpy tutorial › numpy for loop
NumPy for loop | Learn the Examples of NumPy for loop
April 3, 2023 - In this example, we’ll use the nditer() function and iterate through individual values by changing their data type while iterating this method is very useful in changing the data types of the array values. ... import numpy as np arr = np.array...
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 62599397 › how-create-numpy-array-with-for-loop
python - how create numpy array with for loop - Stack Overflow
Instead of looping explicitly, use numpy’s vectorised operations: def carr(inte, numb): return numpy.array(range(numb)) * inte · Incidentally, in R you’d do something equivalent (along the lines of seq(n) * x): R uses loops even less than Python; and what you suggested would be inefficient and unnecessarily convoluted in R.
🌐
TutorialsPoint
tutorialspoint.com › numpy › numpy_iterating_over_array.htm
NumPy - Iterating Over Array
This approach allows for more precise control over element access and manipulation compared to simple element iteration. ... In the following example, we iterate over each element of the 2D array using nested loops. We access and print each element's value along with its indices − · import numpy as np # Create a 2-dimensional NumPy array arr_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # Get the dimensions of the array rows, cols = arr_2d.shape # Iterate over the array using indices for i in range(rows): for j in range(cols): print(f"Element at ({i}, {j}): {arr_2d[i, j]}")
🌐
W3Schools
w3schools.com › python › numpy › numpy_array_iterating.asp
NumPy Array Iterating
It solves some basic issues which we face in iteration, lets go through it with examples. In basic for loops, iterating through each scalar of an array we need to use n for loops which can be difficult to write for arrays with very high ...