The numpy array you are defining is not the right shape for the loop you are using. b = np.array([]) gives you an array of shape (0,)

You can use something like np.zeros to define your 2D array.

import numpy as np
b = np.zeros((3,6))
for i in range(2):
    for j in range(5):
        b[i][j]=i+j
print(b)

The output will be

[[0. 1. 2. 3. 4. 0.]
 [1. 2. 3. 4. 5. 0.]
 [0. 0. 0. 0. 0. 0.]]

Another option is to make a 2D list, fill it up in for loops, and convert to numpy array later

import numpy as np

#2D array
b = [ [0 for _ in range(6)] for _ in range(3)]

#Fill up array with for loops
for i in range(2):
    for j in range(5):
        b[i][j]=i+j

#Convert to numpy array
print(np.array(b))

The output will be

[[0 1 2 3 4 0]
 [1 2 3 4 5 0]
 [0 0 0 0 0 0]]
Answer from Devesh Kumar Singh on Stack Overflow
Discussions

How to create 2d NumPy array with for loop [Python] - Stack Overflow
Get a problem There is a double cycle, on the secondary loop we get an array of 10 elements. Then, after each cycle (first), you need to add these elements to the array. At the output, you need to ... More on stackoverflow.com
🌐 stackoverflow.com
May 17, 2018
python - Create a dynamic 2D numpy array on the fly - Stack Overflow
I am having a hard time creating a numpy 2D array on the fly. So basically I have a for loop something like this. for ele in huge_list_of_lists: instance = np.array(ele) creates a 1D numpy ar... More on stackoverflow.com
🌐 stackoverflow.com
python - How to generate 2d numpy array? - Stack Overflow
I, as expected, get a np.array of np.array. But I want not this, but ndarray, so I can get, for example, column in a way like this: ... So, I'm curious whether there is a way to generate it in such a way. Of course it is possible with creating npdarray of required size and filling it with required values, but I want a way to do so in a line of code. ... what do f(a) and g(b) do exactly? If they produce numbers, your code should work: that is the correct way to initialize a 2d numpy ... More on stackoverflow.com
🌐 stackoverflow.com
python - Initialize 2D array within loop - Stack Overflow
How to populate 2D array within the loop? ... How to append that it would make 2D array... np.append doesn't work... ... Stick with list append that you show at the start. Then make the array from the nested list. ... Forget np.append. Learn to use np.concatenate instead. More on stackoverflow.com
🌐 stackoverflow.com
August 7, 2017
Find elsewhere
🌐
NumPy
numpy.org › doc › stable › user › basics.creation.html
Array creation — NumPy v2.5 Manual
vander(x, n) defines a Vandermonde matrix as a 2D NumPy array. Each column of the Vandermonde matrix is a decreasing power of the input 1D array or list or tuple, x where the highest polynomial order is n-1. This array creation routine is helpful in generating linear least squares models, as such:
🌐
Reddit
reddit.com › r/learnpython › how can i create multiple arrays using a for loop or to convert into a 2d array
r/learnpython on Reddit: How can I create multiple arrays using a for loop or to convert into a 2D array
May 20, 2020 -

I have this two arrays

array1 = [1. 0.91 0.8 0.68 0.61 0.57 0.52 0.46 0.39 0.3 ]

array2 = [188.38188649 201.69718164 210.51306563 221.65195842 229.91542541 238.11787625 257.072488 ]

I need to multiple the second array by each value in the first one. I have tried using a for loop like this

for i in range (array1):
    new_array = array2 * array1[i]

But it shows me this error

TypeError: only integer scalar arrays can be converted to a scalar index

Any help is appreciated, thank you.

🌐
Finxter
blog.finxter.com › home › learn python blog › how to create a two dimensional array in python?
How To Create a Two Dimensional Array in Python? - Be on the Right Side of Change
June 11, 2022 - Numpy once again has the solution to your problem as you can use the numpy.arrange() method to reshape a list into a 2D array.
🌐
NumPy
numpy.org › devdocs › user › absolute_beginners.html
NumPy: the absolute basics for beginners — NumPy v2.6.dev0 Manual
Using np.newaxis will increase the dimensions of your array by one dimension when used once. This means that a 1D array will become a 2D array, a 2D array will become a 3D array, and so on.
🌐
W3Schools
w3schools.com › python › numpy › numpy_array_iterating.asp
NumPy Array Iterating
import numpy as np arr = np.array([1, 2, 3]) for x in np.nditer(arr, flags=['buffered'], op_dtypes=['S']): print(x) Try it Yourself » · We can use filtering and followed by iteration.
🌐
Tudelft
nsweb.tn.tudelft.nl › ~gsteele › TN2513_Tips › Creating a numpy array in a loop.html
Creating a numpy array in a loop
Numpy also has an append function, but it does not append to a given array, it instead creates a new array with the results appended. This is both memory inefficient, and also computationally inefficient. You should only use np.append if you really want a second copy of the array. Here below though is how you would use np.append in the loop:
🌐
Sentry
sentry.io › sentry answers › python › define a two-dimensional array in python
Python Define a Two-Dimensional Array or Matrix | Sentry
September 23, 2026 - Create a 2D array in Python using numpy.zeros for matrix operations, or a nested list comprehension if you want to avoid the numpy dependency
🌐
GeeksforGeeks
geeksforgeeks.org › python › numpy-iterating-over-array
Numpy - Iterating Over Arrays - GeeksforGeeks
July 23, 2025 - import numpy as np # Create a 2D array arr_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # Iterate over rows for row in arr_2d: print(row) Output · [1 2 3] [4 5 6] [7 8 9] Explanation: arr_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) creates a 2D array with three rows and three columns. for row in arr_2d: loops through each row of the 2D array, treating it as a separate one-dimensional array...
🌐
Stack Overflow
stackoverflow.com › questions › 59828338 › numpy-how-to-populate-a-2d-array-with-nested-for-loops
python - NUMPY: How to populate a 2D array with nested 'for' loops - Stack Overflow
So I want to know how to fill a 2d Numpy array using a nested for loops. Here is a snippet of my code below: import numpy as np y=np.ones(6000).reshape(100,60) x=1e-5*np.ones(6000).reshape(100,60...