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
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 160559 › empty-2d-array
python - Empty 2D Array [SOLVED] | DaniWeb
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.
🌐
Sentry
sentry.io › sentry answers › python › define a two-dimensional array in python
Define a two-dimensional array in Python | Sentry
The following code will create ... this operation. To create a 2D array without using numpy, we can initialize a list of lists using a list comprehension....
🌐
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....
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-initialize-empty-array-of-given-length
Python - Initialize empty array of given length - GeeksforGeeks
... # initializes all the 10 spaces ...itialising empty list of None: ", b) # initializes a 4 by 3 array matrix all with 0's c = [[0] * 4] * 3 print("Intitialising 2D empty list of zeros: ", c) # empty list which is not null, it's ...
Published   July 12, 2025
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-using-2d-arrays-lists-the-right-way
Using 2D arrays/lists in Python - GeeksforGeeks
This article focuses on correct and incorrect ways to create 1D and 2D lists in Python. A 1D list stores elements in a linear sequence. Although Python does not have a native 1D array type, lists serve the same purpose efficiently. Manually initializing and populating a list without using any advanced features or constructs in Python is known as creating a 1D list using "Naive Methods".
Published   December 20, 2025
🌐
Python Guides
pythonguides.com › create-an-empty-array-in-python
Ways to Initialize an Empty Python Array
December 23, 2024 - List comprehensions provide a more flexible way to create lists of a specific size with custom initialization. Here is an example. # Creating a list of size 5 with default value 0 using list comprehension array_of_zeros = [0 for _ in range(5)] print(array_of_zeros) # Output: [0, 0, 0, 0, 0] # Creating a list of size 5 with indices as values array_of_indices = [i for i in range(5)] print(array_of_indices) # Output: [0, 1, 2, 3, 4] I executed the above Python code, and you can see the output in the screenshot below:
Find elsewhere
🌐
W3docs
w3docs.com › python
How to initialize a two-dimensional array in Python?
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: # create a 2D array with 3 rows and 4 columns, ...
Top answer
1 of 16
611

That is the wrong mental model for using NumPy efficiently. NumPy arrays are stored in contiguous blocks of memory. To append rows or columns to an existing array, the entire array needs to be copied to a new block of memory, creating gaps for the new elements to be stored. This is very inefficient if done repeatedly.

Instead of appending rows, allocate a suitably sized array, and then assign to it row-by-row:

>>> import numpy as np

>>> a = np.zeros(shape=(3, 2))
>>> a
array([[ 0.,  0.],
       [ 0.,  0.],
       [ 0.,  0.]])

>>> a[0] = [1, 2]
>>> a[1] = [3, 4]
>>> a[2] = [5, 6]

>>> a
array([[ 1.,  2.],
       [ 3.,  4.],
       [ 5.,  6.]])
2 of 16
149

A NumPy array is a very different data structure from a list and is designed to be used in different ways. Your use of hstack is potentially very inefficient... every time you call it, all the data in the existing array is copied into a new one. (The append function will have the same issue.) If you want to build up your matrix one column at a time, you might be best off to keep it in a list until it is finished, and only then convert it into an array.

e.g.


mylist = []
for item in data:
    mylist.append(item)
mat = numpy.array(mylist)

item can be a list, an array or any iterable, as long as each item has the same number of elements.
In this particular case (data is some iterable holding the matrix columns) you can simply use


mat = numpy.array(data)

(Also note that using list as a variable name is probably not good practice since it masks the built-in type by that name, which can lead to bugs.)

EDIT:

If for some reason you really do want to create an empty array, you can just use numpy.array([]), but this is rarely useful!

🌐
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.
🌐
Delft Stack
delftstack.com › home › howto › python › how to initiate 2 d array in python
How to Initiate 2-D Array in Python | Delft Stack
February 2, 2024 - ... import numpy dim_columns = 2 dim_rows = 2 output = numpy.full((dim_columns, dim_rows), 0).tolist() print(output) The numpy.full() function of the NumPy will create an array and the tolist() function of NumPy will convert that array to a ...
Top answer
1 of 16
1261

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
486

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.

🌐
Quora
quora.com › How-do-you-create-an-empty-2D-list-in-Python
How to create an empty 2D list in Python - Quora
Answer (1 of 10): C̲r̲e̲a̲t̲i̲n̲g̲ ̲a̲n̲ e̲m̲p̲t̲y̲ ̲2̲D̲ ̲li̲s̲t̲ ̲i̲n̲ ̲P̲y̲t̲h̲o̲n̲ ̲i̲s̲ ̲s̲t̲r̲a̲i̲g̲h̲t̲f̲o̲r̲w̲ar̲d̲ ̲,̲ ̲b̲u̲t̲ ̲u̲n̲d̲e̲r̲s̲t̲a̲n̲di̲n̲g̲ ̲t̲h̲e̲ ̲n̲u̲a̲n̲c̲e̲s̲ ̲e̲ns̲u̲r̲e̲s̲ ̲y̲o̲u̲ ̲a̲vo̲i̲d̲ ̲c̲o̲m̲m̲o̲n̲ ̲p̲i̲t̲f̲a̲l̲l̲s̲ ̲.̲ ̲L̲e̲t̲’̲s̲ ̲b̲r̲e̲a̲k̲ ̲d̲o̲w̲n̲ ̲t̲he̲...
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.empty.html
numpy.empty — NumPy v2.4 Manual
Array of uninitialized (arbitrary) data of the given shape, dtype, and order. Object arrays will be initialized to None. ... Return an empty array with shape and type of input.
🌐
Statistics Globe
statisticsglobe.com › home › python programming language for statistics & data science › create empty 2d list in python (2 examples)
Create Empty 2D List in Python (2 Examples) | Zero Elements
March 31, 2023 - Like the previous example, we initialized 3 rows and 4 columns for the empty 2D list. Then, with the list comprehension method, we ran iterations that created 3 rows and 4 columns that were populated with the “None” value and were then stored ...
🌐
Python Forum
python-forum.io › thread-1818.html
Creating 2D array without Numpy
I want to create a 2D array and assign one particular element. The second way below works. But the first way doesn't. I am curious to know why the first way does not work. Is there any way to create a zero 2D array without numpy and without loop? ...
🌐
NumPy
numpy.org › devdocs › user › absolute_beginners.html
NumPy: the absolute basics for beginners — NumPy v2.5.dev0 Manual
Or even an empty array! The function empty creates an array whose initial content is random and depends on the state of the memory.
🌐
Python Examples
pythonexamples.org › python-numpy-create-2d-array
Create 2D Array in NumPy
import numpy as np # create a 2D array with shape (3, 4) shape = (3, 4) arr = np.empty(shape) print(arr) ... In this NumPy Tutorial, we learned how to create a 2D numpy array in Python using different NumPy functions.