To initialize a two-dimensional list in Python, use

t = [ [0]*3 for i in range(3)]

But don't use [[v]*n]*n, it is a trap!

>>> a = [[0]*3]*3
>>> a
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
>>> a[0][0]=1
>>> a
[[1, 0, 0], [1, 0, 0], [1, 0, 0]]
Answer from Jason CHAN on Stack Overflow
🌐
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
Discussions

how would you instantiate a two-dimensional array in the constructor?
I assume by 2d array you mean nested lists. Also you shouldn't use mutable types as default argument. I would do something like this: class MyClass: def __init__(self, matrix=None): self.matrix = matrix or [['some', 'default'], ['data', ['...']] More on reddit.com
🌐 r/learnpython
4
1
November 21, 2021
How to initialize a two-dimensional array (list of lists, if not using NumPy) in Python? - Stack Overflow
I'm beginning python and I'm trying to use a two-dimensional list, that I initially fill up with the same variable in every place. I came up with this: def initialize_twodlist(foo): twod_list ... More on stackoverflow.com
🌐 stackoverflow.com
What is the difference between these two methods of initializing a 2D matrix? (One is acting a little strange)
This row = [False] * m matrix = [row] * n creates one list that contains False m times, then creates another list that contains that one list n times. The elements of matrix are not distinct objects, rather they're all references to the same object, and changing one therefore reflects on the others, too. This matrix = [[False for i in range(n)] for j in range(m)] actually creates a list that contains multiple distinct list objects. More on reddit.com
🌐 r/learnpython
15
3
May 27, 2022
python - How to define a two-dimensional array? - Stack Overflow
Coming from other languages: it ... and a 2D-Array. And AFAIK there is no way of having a multi-dimensional-array (or list) in python. Should be said here... ... You're technically trying to index an uninitialized array. You have to first initialize the outer list with ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
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
🌐
Python Guides
pythonguides.com › initialize-a-2d-array-in-python
How to Initialize a 2D Array in Python? (2026 Guide)
September 22, 2026 - The most direct way to initialize a 2D array in Python is to type it out as nested lists.
🌐
Replit
replit.com › home › discover › how to initialize a 2d array in python
How to initialize a 2D array in Python | Replit
Understanding proper techniques for copying lists in Python is essential for avoiding these shallow copy issues. ... For numerical computing, the NumPy library is the standard. You can create a 2D array by passing a list of lists to the np.array() ...
🌐
MLJAR
mljar.com › answers › define-two-dimensional-array-python
Define two-dimensional array in Python
array[2][2] = 5 # valid array[7][2] = 3 # error · This way is much easier. You can use zeros function from numpy to create 2D array with all values set to zero: import numpy array = numpy.zeros((4,4)) REMEMBER Even though the second version is easier, the first one is better for this operation unless we need any matrix operations.
Find elsewhere
🌐
Board Infinity
boardinfinity.com › blog › python-2d-array
Python 2D Array | Board Infinity
August 13, 2025 - After it, we again multiply the array with the rows ®, again this will result in the construction of a 2D list. Now sometimes we need to be careful while using this method as it can cause unexpected behaviours. Each row in this 2D array will reference the same column.
🌐
Scaler
scaler.com › home › topics › 2d array in python
2D Array in Python | Python Two-Dimensional Array - Scaler Topics
May 25, 2026 - There is another syntax for creating a 2D array where initialization (creation of array and addition of elements) is done with a single line of code. Square brackets are the notation used to define the nested lists in this form.
🌐
Stack Overflow
stackoverflow.com › questions › 2397141 › how-to-initialize-a-two-dimensional-array-list-of-lists-if-not-using-numpy-in
How to initialize a two-dimensional array (list of lists, if not using NumPy) in Python? - Stack Overflow
I'm beginning python and I'm trying to use a two-dimensional list, that I initially fill up with the same variable in every place. I came up with this: def initialize_twodlist(foo): twod_list ...
🌐
Reddit
reddit.com › r/learnpython › what is the difference between these two methods of initializing a 2d matrix? (one is acting a little strange)
r/learnpython on Reddit: What is the difference between these two methods of initializing a 2D matrix? (One is acting a little strange)
May 27, 2022 -

In one of my programs, I need to to have a 2D matrix of boolean values initially set to all False.

One method to do it is:

matrix = [[False for i in range(n)] for j in range(m)]

which works fine.

But than I tried something like:

row = [False] * m
matrix = [row] * n

Let's say n = m = 3

Initially, the matrix would be:

[[False, False, False], [False, False, False], [False, False, False]]

now, in the second method, if I set matrix[0][0] = True:

The expected result is:

[[True, False, False], [False, False, False], [False, False, False]]

but what actually happens is:

[[True, False, False], [True, False, False], [True, False, False]]

I figured this out and changed my method of initializing the matrix to the first one in my code. But just out of curiosity, can someone please explain what exactly is happening with method 2?

Thank you.....

P.S: Sorry if this is a stupid question :)

🌐
Snakify
snakify.org › two-dimensional lists (arrays)
Two-dimensional lists (arrays) - Learn Python 3 - Snakify
And suppose you have to set elements of the main diagonal equal to 1 (that is, those elements a[i][j] for which i==j), to set elements above than that diagonal equal to 0, and to set elements below that diagonal equal to 2. That is, you need to produce such an array (example for n==4):
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › 2d array in python
Comprehensive Guide to 2D Arrays in Python: Creation, Access, and Use
November 11, 2024 - Let's explore how to create a 2d list Python and populate it. ... In this approach, you first create an empty list, and then for each row, you create another list to represent the columns.
🌐
GeeksforGeeks
geeksforgeeks.org › python › initialize-matrix-in-python
Initialize Matrix in Python - GeeksforGeeks
July 11, 2025 - Best for uniform initialization. ... Explanation: (for _ in range(rows)) runs three times to create three rows. For each row, [0]*cols creates a new list with two zeros by multiplying the value 0 by the number of columns. Generates a fast, memory-efficient 2D array of zeros using NumPy, ideal for numeric data and large matrices.
🌐
Python Guides
pythonguides.com › create-a-2d-array-in-python
2D Array in Python: Nested Lists and NumPy Examples
May 9, 2025 - Related Python array and NumPy tutorials: Check if a NumPy array is empty · Initialize a 2D array in Python · Iterate through a 2D array in Python · Find the maximum value in an array · Use a nested list, [[0] * cols for _ in range(rows)], or NumPy, np.zeros((rows, cols)).
🌐
AskPython
askpython.com › python › two-dimensional-array-in-python
Two Dimensional Array in Python - AskPython
December 27, 2019 - Output-Append 2D Array · Array slicing is used to access multiple values within an array. Syntax: <slice_array> = <array>[start:stop] array1 = [[1,2,3],[4,5,6,7]] #python array slice array2 = array1[1:3] #index 1 to 2 print(array2) array2 = array1[:1] #index 0 to 1 print(array2) Output: Output-Slicing 2D Array ·
Top answer
1 of 7
1263

You're technically trying to index an uninitialized array. You have to first initialize the outer list with lists before adding items; Python calls this "list comprehension".

# Creates a list containing 5 lists, each of 8 items, all set to 0
w, h = 8, 5
Matrix = [[0 for x in range(w)] for y in range(h)] 

#You can now add items to the list:

Matrix[0][0] = 1
Matrix[6][0] = 3 # error! range... 
Matrix[0][6] = 3 # valid

Note that the matrix is "y" address major, in other words, the "y index" comes before the "x index".

print Matrix[0][0] # prints 1
x, y = 0, 6 
print Matrix[x][y] # prints 3; be careful with indexing! 

Although you can name them as you wish, I look at it this way to avoid some confusion that could arise with the indexing, if you use "x" for both the inner and outer lists, and want a non-square Matrix.

2 of 7
487

If you really want a matrix, you might be better off using numpy. Matrix operations in numpy most often use an array type with two dimensions. There are many ways to create a new array; one of the most useful is the zeros function, which takes a shape parameter and returns an array of the given shape, with the values initialized to zero:

>>> import numpy
>>> numpy.zeros((5, 5))
array([[ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.]])

Here are some other ways to create 2-d arrays and matrices (with output removed for compactness):

numpy.arange(25).reshape((5, 5))         # create a 1-d range and reshape
numpy.array(range(25)).reshape((5, 5))   # pass a Python range and reshape
numpy.array([5] * 25).reshape((5, 5))    # pass a Python list and reshape
numpy.empty((5, 5))                      # allocate, but don't initialize
numpy.ones((5, 5))                       # initialize with ones

numpy provides a matrix type as well, but it is no longer recommended for any use, and may be removed from numpy in the future.

🌐
Google
sites.google.com › rgc.aberdeen.sch.uk › rgcahcomputingrevision › software-design › data-types-and-structures › 2d-arrays
Advanced Higher Computing Revision - 2D Arrays
When declaring a 2 dimensional (2D) array we specify the rows and columns · Python treats this as a list within a list. You will notice that we have initialised each element in the array with a value of 0.
🌐
Medium
zc674.medium.com › creating-2d-arrays-in-python-207b70afe605
Creating 2D Arrays in Python. To create a 2D array in Python, I would… | by RC | Medium
March 19, 2021 - Creating 2D Arrays in Python To create a 2D array in Python, I would like to discuss two ways in this blog. To create a 3*3 array in Python, one very intuitive method would be: >>> m1 = [[1] * 3] * …