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
🌐
Snakify
snakify.org › two-dimensional lists (arrays)
Two-dimensional lists (arrays) - Learn Python 3 - Snakify
In real-world Often tasks have to store rectangular data table. [say more on this!] Such tables are called matrices or two-dimensional arrays. In Python any table can be represented as a list of lists (a list, where each element is in turn a list).
🌐
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.
Published: December 20, 2025
Discussions

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
python - How to define a two-dimensional array? - Stack Overflow
List of lists changes reflected across sublists unexpectedly (18 answers) Closed 2 years ago. I want to define a two-dimensional array without an initialized length like this: ... One does not define arrays, or any other thing. You can, however, create multidimensional sequences, as the answers here show. Remember that python ... More on stackoverflow.com
🌐 stackoverflow.com
Explain 2D Lists
Lists contain things. Lists are things. Therefore, lists can contain lists. That's literally all there is to it. More on reddit.com
🌐 r/learnpython
6
2
August 3, 2014
How to define a two-dimensional list in Python like this? - Stack Overflow
In my understanding, two-dimensional list in Python is just a list of lists, so a two-dimensional list can be defined as follows: a=[[0,0],[1,1]] To get an element of this two-dimensional list, we... More on stackoverflow.com
🌐 stackoverflow.com
People also ask

How do I create a 2D list in Python?
Use [[0] * columns for _ in range(rows)] so each row is a separate list that can be mutated independently.
🌐
pythonpool.com
pythonpool.com › home › tutorials › python 2d lists: create, index, copy, flatten, and loop
Python 2d List: From Basic to Advance
How do I access a value in a Python 2D list?
Use matrix[row][column], with both indexes zero-based, and validate row lengths when the list may be jagged.
🌐
pythonpool.com
pythonpool.com › home › tutorials › python 2d lists: create, index, copy, flatten, and loop
Python 2d List: From Basic to Advance
When should I use NumPy instead of a 2D list?
Use NumPy for large homogeneous numerical arrays, matrix operations, broadcasting, and vectorized calculations; use nested lists for small or irregular dependency-free data.
🌐
pythonpool.com
pythonpool.com › home › tutorials › python 2d lists: create, index, copy, flatten, and loop
Python 2d List: From Basic to Advance
🌐
Processing
py.processing.org › tutorials › 2dlists
Two-Dimensional Lists \ Tutorials
Python Mode for Processing extends the Processing Development Environment with the Python programming language.
🌐
Beauty and Joy of Computing
bjc.edc.org › March2019 › bjc-r › cur › programming › old-labs › python › 2D_lists.html
2D Lists in Python
A list within another list is called 2-Dimensional (2D). And just like in a 2D cartesian graph, retrieving an element requires two index values (essentially "the x and y position").
Top answer
1 of 16
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 16
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.

Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python-using-2d-arrays-lists-the-right-way
Python | Using 2D arrays/lists the right way - GeeksforGeeks
... Here we are multiplying the number of rows by the empty list and hence the entire list is created with every element zero. ... Using 2D arrays/lists the right way involves understanding the structure, accessing elements, and efficiently ...
Published: June 20, 2024
🌐
Python Pool
pythonpool.com › home › tutorials › python 2d lists: create, index, copy, flatten, and loop
Python 2d List: From Basic to Advance
July 14, 2026 - Quick answer: A Python 2D list is a list of row lists. Create independent rows with a list comprehension, such as [[0] * columns for _ in range(rows)]; avoid [[0] * columns] * rows because every row then refers to the same list.
🌐
Computer Science Newbies
csnewbs.com › python-8b-2d-lists
Python | 8b - 2D Lists | CSNewbs
Lists can be given another dimension to hold data that is related to each other. ... A scenario: Three students have taken two Chemistry tests, and their teacher has recorded the results in a 2-dimensional array (note that Python does not use ...
🌐
Dot Net Perls
dotnetperls.com › 2d-python
Python - 2D List Examples - Dot Net Perls
# Step 1: create a list. # ... Append empty lists in first two indexes. elements = [] elements.append([]) elements.append([]) # Step 2: add elements to empty lists.
🌐
Programmingforlovers
programmingforlovers.com › home › chapter 3: discovering a self-replicating automaton with top-down programming › chapter 3 python code alongs › introduction to two-dimensional arrays in python
Introduction to Two-Dimensional Arrays in Python - Programming for Lovers
June 22, 2024 - In this code along, we will prepare to implement cellular automata, which are represented as two-dimensional arrays. To do so, we will learn how to extend Python’s list syntax to implement two- and multi-dimensional arrays.
🌐
Python Central
pythoncentral.io › how-to-initialize-a-2d-list-in-python
How to initialize a 2D List in Python? | Python Central
December 29, 2021 - List Comprehension is used to return a list. Python 2D list consists of nested lists as its elements. Let’s discuss each technique one by one. This technique uses List Comprehension to create Python 2D list.
Top answer
1 of 3
3

That is, indexes should be separated by [], instead of being putting together in one [] separated by comma.

a[0,1] is equivalent to a[(0, 1)], which calls a.__getitem__((0, 1)).

a[0][1] is equivalent to a.__getitem__(0).__getitem__(1). As you can see, the brackets are really just a nice way of calling __getitem__.

Python lists only support integers and slice objects as the arguments to __getitem__, so you can't write a[0, 1]. You can, however, write your own class and have __getitem__ do whatever you want:

>>> class Something(object):
...     def __getitem__(self, arg):
...         return arg
...     
>>> Something()[{1, 2, 3}, {4, 5, 6}, 'foo', ..., 12, 4:2]
    ({1, 2, 3}, {4, 5, 6}, 'foo', Ellipsis, 12, slice(4, 2, None))
2 of 3
3

Even though Python lists do not have more than one dimension the notation with two or more indices / slices is needed for arrays / matrices which come with numpy. Even though it (intentionally) doesn't belong to the core library it has become a de facto standard for n-dimensional arrays.

Here you can type

>>> import numpy as np
>>> ar = np.array([[1,2],[3,4]])
>>> ar[0,0]
1
>>> ar[:,0]
array([1, 3])

>>> random_array = np.random.random((100,100))
>>> random_array[50:60,30:35]
array([[ 0.8352567 ,  0.14901839,  0.2409099 ,  0.88278442,  0.84300552],
       [ 0.88403713,  0.54964811,  0.83500869,  0.88258427,  0.90273584],
       [ 0.00271817,  0.94116153,  0.6282039 ,  0.3243262 ,  0.71785796],
       [ 0.0661821 ,  0.99243509,  0.5888741 ,  0.04161134,  0.89517395],
       [ 0.87419943,  0.14761041,  0.06123542,  0.8139316 ,  0.66220133],
       [ 0.24710625,  0.02305463,  0.7301232 ,  0.11279152,  0.57674316],
       [ 0.9893136 ,  0.9711931 ,  0.12936097,  0.49021876,  0.24834283],
       [ 0.48277394,  0.76470469,  0.29348414,  0.43578663,  0.69670601],
       [ 0.43401812,  0.14714134,  0.52015761,  0.40088974,  0.25203087],
       [ 0.9431969 ,  0.04824567,  0.98400652,  0.1129802 ,  0.25518842]])

Custom classes seem to be a very special use case - numpy arrays are really used a lot, almost no scientific Python library does not use numpy.

🌐
Python.org
discuss.python.org › python help
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.
🌐
TutorialsPoint
tutorialspoint.com › python_data_structure › python_2darray.htm
Python - 2-D Array
To print out the entire two dimensional array we can use python for loop as shown below.
🌐
Kosbie
kosbie.net › cmu › fall-11 › 15-112 › handouts › notes-2d-lists.html
2d Lists
# 2d lists do not really exist in Python. # They are just lists that happen to contain other lists as elements. # And so this can be done for "3d lists", or even "4d" or higher-dimensional lists. # And these can also be non-rectangular, of course!