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

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
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
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
Two Dimensional List - dynamically creating it does not work, while manually works fine.
As you can see, multiplying just gives you several references to the same sublist. Instead you should loop, eg with a list comprehension: self.grid = [[0] * self.sizeY for _ in range(self.sizeX)] More on reddit.com
🌐 r/learnpython
4
2
January 8, 2024
🌐
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
🌐
CodeGenes
codegenes.net › blog › how-to-initialize-a-2d-array-in-python
Initializing 2D Arrays in Python — codegenes.net
numpy is a powerful library in Python for scientific computing. It provides a more efficient way to work with multi-dimensional arrays. If you haven't installed numpy yet, you can install it using pip: ... import numpy as np # Create a 3x3 2D array filled with zeros two_d_array = np.zeros((3, 3)) print(two_d_array)
🌐
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:
🌐
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
🌐
Replit
replit.com › home › discover › how to initialize a 2d array in python
How to initialize a 2D array in Python | Replit
Without a copy, modifying one row would accidentally change all of them, since they would all reference the same list object. 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() function.
Find elsewhere
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.

🌐
Board Infinity
boardinfinity.com › blog › python-2d-array
Python 2D Array | Board Infinity
August 13, 2025 - This means if we try to update only a single element of the array, it will update the same column in our array. ... In the above code, we are using the list comprehension technique, in this technique we use a loop for a list and inside a list, ...
🌐
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 ...
🌐
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.
🌐
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 - To create a 3*3 array in Python, ... change one element in m1 and m2, for example, change the element in the position of first column second row to 3, and print both of them out....
🌐
NumPy
numpy.org › devdocs › user › absolute_beginners.html
NumPy: the absolute basics for beginners — NumPy v2.6.dev0 Manual
This means that a 1D array will become a 2D array, a 2D array will become a 3D array, and so on. ... You can explicitly convert a 1D array to either a row vector or a column vector using np.newaxis. For example, you can convert a 1D array to a row vector by inserting an axis along the first dimension:
🌐
Scaler
scaler.com › home › topics › 2d array in python
2D Array in Python | Python Two-Dimensional Array - Scaler Topics
May 25, 2026 - Let's see some examples of how elements are accessed in a 2D array in python. In this example, the following code first initializes an array named arr with the given values, then accesses a single element of the arr array present at the 3rd column in the second row.
🌐
TutorialsPoint
tutorialspoint.com › python_data_structure › python_2darray.htm
Python - 2-D Array
The example below illustrates how it works. from array import * T = [[11, 12, 5, 2], [15, 6,10], [10, 8, 12, 5], [12,15,8,6]] print(T[0]) print(T[1][2]) When the above code is executed, it produces the following result − ... To print out the ...
🌐
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.
🌐
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 - Here's an example: ... # Creating a 2-D list using nested lists rows, cols = 3, 4 matrix = [] # Initialize an empty list to represent the 2-D list for i in range(rows): row = [] # Create an empty list for each row for j in range(cols): row.append(i ...
🌐
AskPython
askpython.com › python › two-dimensional-array-in-python
Two Dimensional Array in Python - AskPython
December 27, 2019 - 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 ·
🌐
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.