More "modern python" way of doing things.

[[ randint(0,4) for x in range(0,4)] for y in range(0,4)]

Its simply a pair of nested list comprehensions.

Answer from Shayne on Stack Overflow
๐ŸŒ
Snakify
snakify.org โ€บ two-dimensional lists (arrays)
Two-dimensional lists (arrays) - Learn Python 3 - Snakify
The thing is, if the number 0 is replaced by some expression that depends on i (the line number) and j (the column number), you get the matrix filled according to some formula. For example, suppose you need to initialize the following array (for convenience, extra spaces are added between items):
Top answer
1 of 2
1

Are you necessarily looking to use for-loops? I am asking because this problem can be solved in simpler and more efficient ways such as:

import numpy as np

a_values = np.random.rand(20,402) #Store random values in a_values with a shape of 20 rows and 402 columns
avg_columns = a_values.mean(axis=0) #Calculate the mean of each column 

print(avg_columns)

Documentation for generating random values: numpy.random.rand

EDIT:

I assumed that xa is randomized in each iteration (make sure to replace it with the xa that you are generating). You can initialize an empty a_values array, and add the generated xa array to it in each iteration using np.vstack. For the first iteration, when a_value is empty, I made it equal to xa (if a_values.size else xa).

import numpy as np

a_values = np.array([])

for i in range(20):
  xa = np.random.rand(1,402)
  a_values = np.vstack([a_values, xa]) if a_values.size else xa

avg_columns = a_values.mean(axis=0) #Average of each column

Documentation for vstack: numpy.vstack

2 of 2
1

I don't think you can change the shape of A_values after it's defined. So you should start with:

A_values = numpy.zeros([20,402])

And if your for loop is iterating through the rows, your index should be [i, 1] (numpy array indexes are not like cartesian plane coords).

But you want to change entire rows of A_values, to do this you use just the first index:

A_values[row] = some_row

Remember that "some_row" needs to have the proper lenght, otherwise you get a ValueError.

Your final code will look like this:

A_values = numpy.zeros([20, 402])

for i in range(0,20):
    A_values([i]) = xa

PS: You should describe the errors you are getting and be clearer if you want help with the "xa" values as well...

๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 57500627 โ€บ fill-a-numpy-2d-array-with-function-in-one-line
python - fill a numpy 2D array with function in one line - Stack Overflow
I would like to fill a 2D numpy array efficiently based on an operation ยท I would like to do the following in a single line ยท 1DArray = ['A','B','C','D'] N = len(1DArray) 2DArray = np.zeros((N,N)) for i in range(N): for j in range (i, N): 2DArray[i,j] = function(1DArray[i], 1DArray[j]) python ยท
๐ŸŒ
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
๐ŸŒ
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 - Now that you have a clear picture ... 2D array loaded with zeros, for every occurrence of a row, we fill all the column elements and append that to the row....
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.1 โ€บ reference โ€บ generated โ€บ numpy.ndarray.fill.html
numpy.ndarray.fill โ€” NumPy v2.1 Manual
>>> import numpy as np >>> a = np.array([1, 2]) >>> a.fill(0) >>> a array([0, 0]) >>> a = np.empty(2) >>> a.fill(1) >>> a array([1., 1.])
Find elsewhere
๐ŸŒ
AskPython
askpython.com โ€บ python โ€บ two-dimensional-array-in-python
Two Dimensional Array in Python - AskPython
December 27, 2019 - So, Python does all the array related operations using the list object.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-using-2d-arrays-lists-the-right-way
Using 2D arrays/lists in Python - GeeksforGeeks
The code below, compares two ways of initializing a 2D list in Python. Using list multiplication ([[0]*cols]*rows) creates multiple references to the same inner list, causing aliasing where changes affect all rows. Using a nested list comprehension creates a separate list for each row, avoiding aliasing and correctly forming a 2D array.
Published: December 20, 2025
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 64920081 โ€บ question-about-filling-a-2d-array-in-python
Question about filling a 2D array in Python - Stack Overflow
What I want is to fill a 3*3 array. This code works as intended: arr = [ [] ] * 3 for i in range(3): arr[i] = (list(map(int, input().split()[:3]))) print(arr) But if I write it like this: arr...
๐ŸŒ
Python Guides
pythonguides.com โ€บ python-numpy-2d-array
Create A 2D NumPy Array In Python (5 Simple Methods)
May 9, 2025 - Create and use a 2D array in Python with nested lists and NumPy: zeros, reshape, indexing rows and columns, adding rows, user input and the [[0]*n]*m trap.
๐ŸŒ
Medium
medium.com โ€บ @amit25173 โ€บ what-is-numpy-fill-and-when-to-use-it-6c6192c67b9d
What is numpy.fill and When to Use It? | by Amit Yadav | Medium
February 9, 2025 - You start with an array containing [1, 2, 3, 4, 5]. The fill() method replaces all the values with 0.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 61124174 โ€บ how-to-fill-each-element-of-a-2d-array-with-a-for-loop-in-python
How to fill each element of a 2d array with a for loop in python - Stack Overflow
How to make it such that once the first element is filled, the for loop moves on to the next element and the next value? ... sim_matrix=np.zeros((3,3), dtype=float) for i in range(0,3): for j in range(0,3): for k in range(0,3): for l in range(0,3): sim_matrix[i][j]=node_list_bsc[0][k]['bsc mean value']-node_list_bsc[1][l]['bsc mean value'] #node_list_bsc[x][y]['bsc mean value'] are float values - they are stored in a list of dictionaries, #hence the double indexing ... array([[1.14488261e-08, 1.14488261e-08, 1.14488261e-08], [1.14488261e-08, 1.14488261e-08, 1.14488261e-08], [1.14488261e-08, 1.14488261e-08, 1.14488261e-08]])
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 74916474 โ€บ fill-2d-array-python-value-from-a-list-1d-array
fill 2d array python value from a list / 1d array - Stack Overflow
December 25, 2022 - OR put directly the b value into 2D array whic one colum represent loop for number of k. *My difficulties here is, k is not integer. its dict keys (str). whose length=9 ... row = len(data.items()) matrix=np.zeros((9,2)) for i in range (1,3) : a=[] for k, v in data.items(): b=v/sumcount matrix[x][i].fill(b), for x in range (1, 10)