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
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 ...
Published   December 20, 2025
Discussions

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
0
June 3, 2022
how would you instantiate a two-dimensional array in the constructor?
This is a default argument. And yes you can do something like that, but it's not recommended. Default to None instead, and check inside the function if it's None. https://docs.python-guide.org/writing/gotchas/#mutable-default-arguments More on reddit.com
🌐 r/learnpython
4
1
November 21, 2021
How can I create a truly empty numpy array which can be merged onto (by a recursive function)?
I can't say I fully followed your problem statement, but you can create an array with a total size of zero if any of the dimensions has size zero: a = np.empty((0, 3)) # Doesn't really matter if you use `empty`, `zeros` or `ones` here Zero-size arrays are the neutral element wrt. concatenation along their zero-size dimension (if that's what you mean by "merging"): b = np.random.uniform(size=(20, 3)) c = np.concatenate([a, b], 0) (c == b).all() # True More on reddit.com
🌐 r/learnpython
2
2
June 21, 2023
list - Creating a 2d matrix in python - Stack Overflow
How to initialize a two-dimensional array (list of lists, if not using NumPy) in Python? (31 answers) Closed last year. I create a 6x5 2d array, initially with just None in each cell. I then read a file and replace the Nones with data as I read them. I create the empty array first because the ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
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.
🌐
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.
🌐
Sentry
sentry.io › sentry answers › python › define a two-dimensional array in python
Define a two-dimensional array in Python | Sentry
June 15, 2023 - 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....
🌐
MLJAR
mljar.com › answers › define-two-dimensional-array-python
Define two-dimensional array in Python
w = 6 # width, number of columns h = 4 # height, number of rows array = [[0 for x in range(w)] for y in range(h)] ... REMEMBER Be careful with idexing, you can't go out of range! ... This way is much easier. You can use zeros function from numpy to create 2D array with all values set to zero:
🌐
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.
Find elsewhere
🌐
W3Schools
w3schools.com › python › numpy › numpy_creating_arrays.asp
NumPy Creating Arrays
In this array the innermost dimension (5th dim) has 4 elements, the 4th dim has 1 element that is the vector, the 3rd dim has 1 element that is the matrix with the vector, the 2nd dim has 1 element that is 3D array and 1st dim has 1 element that is a 4D array. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
🌐
NumPy
numpy.org › doc › 2.3 › reference › generated › numpy.empty.html
numpy.empty — NumPy v2.3 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.
🌐
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? ...
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › numpy empty array with examples
NumPy Empty Array With Examples - Spark By {Examples}
March 27, 2024 - NumPy empty() array function in Python is used to create a new array of given shapes and types, without initializing entries. This function takes three arguments, we can customize the specific data type and order by passing these parameters.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-initialize-empty-array-of-given-length
Python - Initialize empty array of given length - GeeksforGeeks
# initialize the spaces with 0’s with # the help of list comprehensions a = [0 for x in range(10)] print(a) b = [[0] * 4 for i in range(3)] print(b) ... In this example, we are using a Python loop for 1D and 2D empty arrays.
Published   July 12, 2025
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.empty.html
numpy.empty — NumPy v2.1 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.
🌐
Reddit
reddit.com › r/learnpython › how can i create a truly empty numpy array which can be merged onto (by a recursive function)?
r/learnpython on Reddit: How can I create a truly empty numpy array which can be merged onto (by a recursive function)?
June 21, 2023 -

I'm kind of stuck conceptually on how to make this happen. I have a recursive method that builds a binary tree, and stores the tree as an instance variable. However, the function is not allowed to return anything, so each recursive call should (according to me) modify in-place the tree instance variable. However, I'm not sure how to set up my instance variable such that all said and done it holds a multidimensional array that represents the tree.

Say I set initialize it as a 1x1 array with element zero as a placeholder. Then as I go about recursing through my tree I can merge to it... but at the end I'm left with a spare [0] element that I don't need. In this case, I'd need some kind of final stop condition and function to remove that unnecessary placeholder stump. I don't think this is possible?

Otherwise, say I initialize the instance variable as None. Then when the first series of recursive calls, it would have to reassign the tree variable to change from None to an ndarray object, but all future calls would have to merge to the array. I don't think this is what the function should be asked to do?

Is there a way to make a truly empty array that I can merge onto? (e.g. np.empty doesn't reallly give an empty array, it gives an array with placeholder values so I'm still left with a useless stump at the end).

🌐
YouTube
youtube.com › reallifeed
Python 2D arrays and lists - YouTube
How to use 2D Arrays and Lists. Python Programming Beginners series.In this video:- 2D Arrays- 2D ListsTools:The Python Standard Library - https://docs.pyth...
Published   October 24, 2022
🌐
W3docs
w3docs.com › python
How to initialize a two-dimensional array in Python?
Empty Lines Remover · HTML Tags ... 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....
🌐
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 - This technique uses List Comprehension to create Python 2D list. Here we use nested List comprehension to initialize a two-dimensional list.
🌐
Plain English
python.plainenglish.io › working-with-2d-arrays-in-python-3a13c574292b
Working with 2D arrays in Python. 2-dimensional arrays and how to work… | by Naveenkumar M | Python in Plain English
August 4, 2021 - Accessing the elements is very easy. let's think of a 2d array as a list that contains some more lists in it. Consider the inner lists as a single variable. Thus we can get the elements by using “arr[i][j]” ... I have included all the basics If you find something wrong or like to add something feels free to comment ... New Python content every day.