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

Cannot initialize series with empty 2D numpy array
bugSomething isn't workingSomething ... to Python Polars ... I have checked that this issue has not already been reported. I have confirmed this bug exists on the latest version of Polars. data = np.array([], dtype="2f") # array([], shape=(0, 2), dtype=float32) pl.Series(data) ... Trying to create a Series from a empty numpy 2D array results ... More on github.com
🌐 github.com
1
August 26, 2024
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 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 17, 2023
How to initialize array with one unknow dimension size
I am not able to find any example how to initialize 2D array with one unknown dimension. Normally I use: A = zeros(dim1,dim2) for ii = 1:dim2 A[:,ii] = some_function_returning_vector_of_dim1() end But very often I don’t know the dim1. Please, could you help me? More on discourse.julialang.org
🌐 discourse.julialang.org
0
November 25, 2021
🌐
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....
🌐
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
🌐
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.
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.empty.html
numpy.empty — NumPy v2.2 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.
Find elsewhere
🌐
GitHub
github.com › pola-rs › polars › issues › 18369
Cannot initialize series with empty 2D numpy array · Issue #18369 · pola-rs/polars
August 26, 2024 - bugSomething isn't workingSomething isn't workingneeds triageAwaiting prioritization by a maintainerAwaiting prioritization by a maintainerpythonRelated to Python PolarsRelated to Python Polars ... I have checked that this issue has not already been reported. I have confirmed this bug exists on the latest version of Polars. data = np.array([], dtype="2f") # array([], shape=(0, 2), dtype=float32) pl.Series(data) ... Trying to create a Series from a empty numpy 2D array results in InvalidOperationError: cannot reshape empty array into shape (0, 2).
Published   Aug 26, 2024
🌐
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.
🌐
w3resource
w3resource.com › numpy › array-creation › empty.php
NumPy: numpy.empty() function - w3resource
The dtype parameter in the np.empty() function can be used to specify the data type of the empty array. In the first example, an empty 2D array of size (2, 2) is created with the specified data type int.
🌐
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 17, 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).

🌐
Julia Programming Language
discourse.julialang.org › general usage
How to initialize array with one unknow dimension size - General Usage - Julia Programming Language
November 25, 2021 - I am not able to find any example how to initialize 2D array with one unknown dimension. Normally I use: A = zeros(dim1,dim2) for ii = 1:dim2 A[:,ii] = some_function_returning_vector_of_dim1() end But very ofte…
🌐
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.
🌐
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.
🌐
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