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...

🌐
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....
🌐
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.
🌐
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
Find elsewhere
🌐
W3docs
w3docs.com › home › code snippets › python › how to initialize a two-dimensional array in python?
How to initialize a two-dimensional array in Python? | W3docs
You can use the built-in list function to create a 2D array (also known as a list of lists) in Python. Here is an example of how to create a 2D array with 3 rows and 4 columns, filled with zeroes:
🌐
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
Not sure if other responses have actually been tested, given that python identifiers can't start with a number... As for your question, if you wish to keep the lower tridiagonal part of the matrix equal to zero, you can simply do · array_1d = [1, 2, 3, 4] # or np.array([1, 2, 3, 4]) X, Y = np.meshgrid(array_1d, array_1d) array_2d = np.triu(function(X, Y))
🌐
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 › 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...
🌐
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.])
🌐
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.