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

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
2 different ways to initialise a 2d list give different results
I was initialising a 2d list and was getting strange results. After some furtling around I found that the way I was initialising the list had an unexpected impact. rows =2 columns= 2 Array=[[0]*columns]*rows mylist = [… More on discuss.python.org
🌐 discuss.python.org
2
0
November 11, 2022
Need help with a two-dimensional array
Hello there! I’m an experienced coder who’s just getting into Python for the first time. I know several versions of BASIC, Pascal, C, C++, C#, PHP, MySQL… and so on. Here’s what I have: 1 2 3 4 5 6 7 8 A X X X X X X X X B X X X X X T X X C X X X X X X X X D X X X X X X X X E X X X X ... More on discuss.python.org
🌐 discuss.python.org
4
0
June 3, 2022
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
🌐
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
🌐
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 :)

🌐
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.
🌐
Python.org
discuss.python.org › python help › 2 different ways to initialise a 2d list give different results
2 different ways to initialise a 2d list give different results - Python Help - Discussions on Python.org
November 11, 2022 - I was initialising a 2d list and was getting strange results. After some furtling around I found that the way I was initialising the list had an unexpected impact. rows =2 columns= 2 Array=[[0]*columns]*rows mylist = [[0 for x in range(columns)] for x in range(rows)] print (Array) print (type(Array)) print (type(Array[0])) print (type(Array[0][0])) print (mylist) print (type(mylist)) print (type(mylist[0])) print (type(mylist[0][0])) x=1 for i in range(rows): for j in range(columns): ...
Find elsewhere
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 160559 › empty-2d-array
python - Empty 2D Array [SOLVED] | DaniWeb
December 3, 2008 - For heavy numeric work, prefer NumPy arrays for performance and vectorization; initialize once you know dimensions: arr = numpy.zeros((rows, cols)) (NumPy zeros). More on the aliasing gotcha: Python FAQ: multidimensional lists. defaultdict docs: collections.defaultdict. hoe to write a generic code for creating a empty 2D array and dynamically insert values in it.
🌐
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.
🌐
Quora
quora.com › How-do-you-create-an-empty-multidimensional-array-in-Python
How to create an empty multidimensional array in Python - Quora
Answer (1 of 5): You can’t - a multidimensional list (not array) in Python is a list of lists. if the top level list is empty then it isn’t multidimensional - it is an empty list. if the list on the next level down are empty then you have a list which is N by zero - hardly multi-dimensional.
🌐
Python.org
discuss.python.org › python help › need help with a two-dimensional array
Need help with a two-dimensional array - Python Help - Discussions on Python.org
June 3, 2022 - Hello there! I’m an experienced coder who’s just getting into Python for the first time. I know several versions of BASIC, Pascal, C, C++, C#, PHP, MySQL… and so on. Here’s what I have: 1 2 3 4 5 6 7 8 A X X X X X X X X B X X X X X T X X C X X X X X X X X D X X X X X X X X E X X X X X X X X F X X X X X X X X G X X X X X X X X H X X X X X X X X This is a simple two-dimensional array.
🌐
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.
🌐
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):
🌐
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.
🌐
Princeton University
introcs.cs.princeton.edu › python › 14array
Arrays in Python
We call such an array a 2-by-3 array. More generally, Python represents an m-by-n array as an array that contains m objects, each of which is an array that contains n objects. For example, this Python code creates an m-by-n array a[][] of floats, with all elements initialized to 0.0:
🌐
NumPy
numpy.org › doc › stable › user › basics.creation.html
Array creation — NumPy v2.5 Manual
NumPy arrays can be defined using Python sequences such as lists and tuples. Lists and tuples are defined using [...] and (...), respectively. Lists and tuples can define ndarray creation: a list of numbers will create a 1D array, a list of lists will create a 2D array, further nested lists will create higher-dimensional arrays.
🌐
Medium
medium.com › @izxxr › 2d-arrays-in-python-08ff7c287b2a
2D Arrays in Python. 2D arrays, also known as a matrix, are… | by Izhar Ahmad | Medium
September 7, 2024 - This is because when we use the * operator, it simply creates references of initial elements stored in list. This means all rows in the array are, in fact, the reference to first row.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-arrays
Python Arrays - GeeksforGeeks
Use Python’s array module for simple, memory-efficient storage of uniform data. Array is a collection of elements stored at contiguous memory locations, used to hold multiple values of the same data type. Unlike Lists, which can store mixed types, arrays are homogeneous and require a typecode during initialization to define the data type.
Published: September 19, 2026