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
As is always the case in coding, there are a couple of different ways to do this, we show some of them here. One way to do this is to use a list to collect your values and then convert it at the end to a numpy array. If you like lists, this is quick and handy.
Discussions

Building up an array in numpy/scipy by iteration in Python? - Stack Overflow
That makes sense, but suppose I ... allocate numpy.zeros(1, 1000) but only use 50 elements, I don't want to iterate until I hit a zero... the approach of overallocating seems to create this weird situation where each array data type will require a different "stop" condition for looping... More on stackoverflow.com
🌐 stackoverflow.com
creating a numpy array of arrays while looping (Python) - Stack Overflow
I have a method that returns a numpy array. What I want to do is in each iteration take this array and concatenate it into another array of arrays so that at the end of the loop I have a matrix Th... More on stackoverflow.com
🌐 stackoverflow.com
python - Fast loop to create an array of values - Code Review Stack Exchange
I have a code that creates a 3D array of values from a minimum to a maximum in X and Z with constant Y. Right now I make it in normal Python, and then I transform it in a np.array. Is there a way... More on codereview.stackexchange.com
🌐 codereview.stackexchange.com
April 14, 2017
python - how to use for loop with numpy array? - Stack Overflow
It is about initializing an array with a sequence of numbers, if I am not mistaken. ... You could also skip the first code snippet since it is irrelevant to your actual question. ... There is no need to use a loop. You can use numpy.arange([start, ]stop, [step, ]) to generate a range of numbers. More on stackoverflow.com
🌐 stackoverflow.com
🌐
W3Schools
w3schools.com › python › numpy › numpy_array_iterating.asp
NumPy Array Iterating
import numpy as np arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]) for x in arr: for y in x: for z in y: print(z) Try it Yourself » · The function nditer() is a helping function that can be used from very basic to very advanced iterations. 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 dimensionality.
🌐
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]}")
🌐
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 have created an array and iterated it twice using python for a loop. This method allows us to perform different operations while iterating multiple times, and this method is very efficient and requires less coding. In this example, we have created a zero-dimensional array and converted it into a two-dimensional array. ... import numpy as np arr = np.array([[[8, 16, 44], [0, 4, 7]], [[22, 40, 16], [7, 14, 21]]]) for x in arr: for y in x: for z in y: print(z)
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
Top answer
1 of 4
36

NumPy provides a 'fromiter' method:

def myfunc(n):
    for i in range(n):
        yield i**2


np.fromiter(myfunc(5), dtype=int)

which yields

array([ 0,  1,  4,  9, 16])
2 of 4
19

The recommended way to do this is to preallocate before the loop and use slicing and indexing to insert

my_array = numpy.zeros(1,1000)
for i in xrange(1000):
    #for 1D array
    my_array[i] = functionToGetValue(i)
    #OR to fill an entire row
    my_array[i:] = functionToGetValue(i)
    #or to fill an entire column
    my_array[:,i] = functionToGetValue(i)

numpy does provide an array.resize() method, but this will be far slower due to the cost of reallocating memory inside a loop. If you must have flexibility, then I'm afraid the only way is to create an array from a list.

EDIT: If you are worried that you're allocating too much memory for your data, I'd use the method above to over-allocate and then when the loop is done, lop off the unused bits of the array using array.resize(). This will be far, far faster than constantly reallocating the array inside the loop.

EDIT: In response to @user248237's comment, assuming you know any one dimension of the array (for simplicity's sake):

my_array = numpy.array(10000, SOMECONSTANT)

for i in xrange(someVariable):
    if i >= my_array.shape[0]:
        my_array.resize((my_array.shape[0]*2, SOMECONSTANT))

    my_array[i:] = someFunction()

#lop off extra bits with resize() here

The general principle is "allocate more than you think you'll need, and if things change, resize the array as few times as possible". Doubling the size could be thought of as excessive, but in fact this is the method used by several data structures in several standard libraries in other languages (java.util.Vector does this by default for example. I think several implementations of std::vector in C++ do this as well).

Find elsewhere
🌐
NumPy
numpy.org › doc › stable › reference › arrays.nditer.html
Iterating over arrays — NumPy v2.4 Manual
By default, the nditer uses the flags ‘allocate’ and ‘writeonly’ for operands that are passed in as None. This means we were able to provide just the two operands to the iterator, and it handled the rest. When adding the ‘out’ parameter, we have to explicitly provide those flags, because if someone passes in an array as ‘out’, the iterator will default to ‘readonly’, and our inner loop would fail.
🌐
DataCamp
campus.datacamp.com › courses › intermediate-python › loops
Loop over NumPy array - Intermediate Python
Two NumPy arrays that you might ... the numpy package under the local alias np. Write a for loop that iterates over all elements in np_height and prints out "x inches" for each element, where x is the value in the array....
🌐
NumPy
numpy.org › devdocs › user › c-info.beyond-basics.html
Beyond the basics — NumPy v2.5.dev0 Manual
One common algorithmic requirement ... object makes this easy to do in a generic way that works for arrays of any dimension. Naturally, if you know the number of dimensions you will be using, then you can always write nested for loops to accomplish the iteration...
Top answer
1 of 1
4

You could use the np.fromiter function and Python's built in itertools.product to create the array you need:

Note: I'm assuming you're using Python 2.x based on your print statements.

import itertools
import numpy as np

product = itertools.product(xrange(X, X + 1000*STEP_X, STEP_X),
                            [Y],
                            xrange(Z, Z + 1000*STEP_Z, STEP_Z))

targets = np.fromiter(product)

This should be faster because it uses iterators instead of creating and allocating an entire list.


UPDATE

Here are some style pointers and other minor improvements that I could see. Most of these recommendations stem from PEP8, the official Python style guide, so if you need a reference for my suggestions, you can head over there.

  1. ALWAYS USE with. Whenever you deal with file access, use a with block as it is significantly less prone to user errors than using open() and close(). Luckily, you're code doesn't show the typical bug of not calling close() after an open(). However, its best to get into the habit of using with:

    with open('some_file.txt', 'r') as file: # Do stuff

  2. Use underscores_in_names when naming variables and functions. For the most part you do this. However, your function names could be updated.

  3. Function names should be verb-based as this style helps show that the function does something:

    # Currently...
    def XYZ2sRGB(...):
    
    # Better...
    def convert_to_RGB(...)
    

    A quick note: Typically I don't like using upper-case letters in anything except constants. However, because RGB is basically an acronym, capital letters seem appropriate.

  4. Speaking about upper-case letters, convention says that only constants should be capitalized in Python. This is relatively significant because convention is the only way we can 'define' constants in Python as there is no syntactic way to do so.

  5. Whitespace is your friend, however be careful not to overdo it. PEP8 actually calls extraneous whitespace a pet peeve. A few of the points mentioned in that section of PEP8 that are applicable are:

    # Bad                  # Good
    foo            = 1  |  foo = 1
    some_long_name = 0  |  some_long_name = 0
    --------------------+---------------------
    range (1000)        |  range(1000)
    --------------------+---------------------
    foo = ( x + 1 * 2 ) |  foo = (x + 1*2)
    

    The last example is really based on preference: simply use whitespace to group operations and operands together so that the calculation reads well.

  6. Parenetheses aren't required in if statements (unless they group conditionals together). You can remove almost all of yours.

  7. Use if ... elif ... when applicable. Take this group of statements:

    G = var_G * 255
    if (G > 255):
        G = 255
    if (G < 0):
        G = 0
    

    The second if will always be evaluated even if the first evaluated to True which means the second will evaluate to False. Because the two conditional are mutually exclusive, use and if-elif structure. Also, instead of basing your conditionals off of G (which requires a calculation beforehand) base your conditionals off of var_G:

    if var_G > 1:
        G = 255
    elif var_G < 0:
        G = 0
    else:
        G = var_G * 255
    

    This code only does the calculation if necessary and has the same number of possible comparisions (in the worst case).

  8. Use str.format instead of string concatenation. While whether string formatting performs better than string concatenation is up in the air, its more conventional (and, in my opinion, MUCH cleaner) to use str.format:

    with open(str(filename), "a") as f:
        f.write('<path d="M{} {} \n'.format(x*1/2.54*72, y*1/2.54*72)) #moveto
        f.write('    m {},0 \n'.format(-radius))
        f.write('    a {} 0 1,0 {},0 \n'.format(radius, radius, radius*2))
        f.write('    a {} 0 1,0 {},0 \n'.format(radius, radius, -radius*2))
        f.write('    " fill = "rgb({},{},{})"/> \n'.format(R, G, B))
    
🌐
Stack Overflow
stackoverflow.com › questions › 62599397 › how-create-numpy-array-with-for-loop
python - how create numpy array with for loop - Stack Overflow
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.
🌐
NumPy
numpy.org › doc › 2.1 › reference › arrays.nditer.html
Iterating over arrays — NumPy v2.1 Manual
To make its properties more readily accessible during iteration, nditer has an alternative syntax for iterating, which works explicitly with the iterator object itself. With this looping construct, the current value is accessible by indexing into the iterator.
🌐
Pluralsight
pluralsight.com › tech insights & how-to guides › tech guides & tutorials
Iterating Numpy Arrays | Pluralsight
December 11, 2018 - We will also have a deep dive into the iterator object nditer and the powerful iteration capabilities it offers. First, let’s look at iterating NumPy arrays without using the nditer object. Iterating a one-dimensional array is simple with the use of For loop.
🌐
Numba Discussion
numba.discourse.group › support: how do i do ...?
Creating numpy arrays from arrays - Support: How do I do ...? - Numba Discussion
January 29, 2023 - Hi, in reference to issue: #4470 , are there any workarounds to create numpy arrays from arrays. My sample code #4470 is: import numpy as np import numba @numba.njit def myfunc(mol1Coord,mol2Coord): for j in numba.prange(int(mol2Coord.shape[0]/3)): rIVec=np.array([mol1Coord[0],mol1Coord[0],mol1Coord[0],mol2Coord[3*j],mol2Coord[3*j]], dtype=numba.float64) rJVec=np.array([mol2Coord[3*j],mol2Coord[3*j+1],mol2Coord[3*j+2],mol1Coord[1],mol1Coord[2]], dtype=numba.float64) ...
🌐
NumPy
numpy.org › doc › stable › user › c-info.beyond-basics.html
Beyond the basics — NumPy v2.4 Manual
One common algorithmic requirement ... object makes this easy to do in a generic way that works for arrays of any dimension. Naturally, if you know the number of dimensions you will be using, then you can always write nested for loops to accomplish the iteration...
🌐
Real Python
realpython.com › numpy-array-programming
Look Ma, No for Loops: Array Programming With NumPy – Real Python
November 23, 2023 - When looping over an array or any data structure in Python, there’s a lot of overhead involved. Vectorized operations in NumPy delegate the looping internally to highly optimized C and Fortran functions, making for cleaner and faster Python code.