You should use numpy.zeros. If that isn't an option, you want the first version. In the second version, if you change one value, it will be changed elsewhere in the list -- e.g.:

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

This is because (as you read the expression from the inside out), you create a list of 10 zeros. You then create a list of 10 references to that initial list of 10 zeros.


Note that this will also work and it avoids the nested list comprehension:

zeros = [ [0]*M for _ in range(N) ]

If numpy isn't on the table, this is the form I would use.

(Use xrange if you're still stuck in the python2.x dark ages :).)

Answer from mgilson on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-create-an-array-of-zeros-in-python
How to create an array of zeros in Python? - GeeksforGeeks
July 23, 2025 - Explanation: This code creates ... loop runs 5 times for columns, producing a matrix of zeros. bytearray creates a mutable array of bytes initialized to zero....
🌐
Sentry
sentry.io › sentry answers › python › define a two-dimensional array in python
Define a two-dimensional array in Python | Sentry
We can create a 2D array by passing a tuple to the zeros function. The following code will create a 3-by-3 2D array with all values set to zero: ... Note that numpy.matrix is deprecated and should not be used for this operation.
🌐
Quora
quora.com › How-do-you-create-an-array-of-zeros-in-Python
How to create an array of zeros in Python - Quora
If you don't have numpy you can approximate a 2D matrix with a list of lists. ... Python doesn’t have arrays. It has other things, like lists or generators. For a list, you could use [0, 0] to get an array with 2 zeroes or [0]*12 to get an ...
🌐
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? ...
🌐
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 - number_of_rows = 3 number_of_columns = 2 arr_2d=[] for x in range(number_of_rows): column_elements=[] for y in range(number_of_columns): # Enter the all the values w.r.t to a particular column column_elements.append(0) #Append the column to the array. arr_2d.append(column_elements) print(arr_2d) ... Initializing arrays loaded with zeros is not such a long task if you know how list comprehensions work in Python.
🌐
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:
Find elsewhere
🌐
Python Examples
pythonexamples.org › python-numpy-zeros
Create Array with Zeros in NumPy - Examples
To create a two-dimensional array of zeros, pass the shape i.e., number of rows and columns as a tuple for shape parameter to numpy.zeros() function. In this example, we shall create a numpy array with 3 rows and 4 columns. import numpy as np #create 2D numpy array with zeros a = np.zeros((3, ...
🌐
DataCamp
datacamp.com › doc › numpy › zeros
NumPy zeros()
Ideal for initializing arrays that will be filled with actual data later. Consider memory layout. Use the `order` parameter based on how you plan to access the array elements for performance optimization. Avoid using for small arrays. For very small arrays, the performance gain might be negligible. Consider using direct assignment if clarity is more important. Compare with similar functions. Consider using `numpy.zeros_like` to create an array of zeros with the same shape and type as an existing array, or `numpy.empty` for uninitialized arrays if performance is a priority and zero-initialization is not required.
🌐
Quora
quora.com › How-do-you-initialize-a-matrix-with-all-zeros-in-Python
How to initialize a matrix with all zeros in Python - Quora
Answer: A Zero matix of any size ... print(a) Output-[0 0 0 0] [/code] * Two-Dimensional- [code]import numpy as np a=np.zeros([3,3],dtype=int) print(......
🌐
w3resource
w3resource.com › numpy › array-creation › zeros.php
NumPy: numpy.zeros() function - w3resource
... Returns an ndarray of zeros with the specified shape, data type, and memory layout order. ... In this example, a tuple (3, 2) defines the shape of the array, and np.zeros() creates a 2D array filled with zeros.
🌐
iO Flood
ioflood.com › blog › np-zeros
NP.Zeroes | Using Zeros() In Numpy
January 31, 2024 - Throughout this guide, we’ve explored the np.zeros function in Python’s numpy library. This function is a powerful tool for initializing arrays, offering an efficient way to create ‘blank slate’ data structures ready for your data.
🌐
GeeksforGeeks
geeksforgeeks.org › numpy › numpy-zeros-python
numpy.zeros() in Python - GeeksforGeeks
January 24, 2025 - import numpy as np # Creating a 2D array with 3 rows and 4 columns arr = np.zeros((3, 4)) print(arr)
🌐
Vultr Docs
docs.vultr.com › python › third-party › numpy › zeros
Python Numpy zeros() - Create Zero Array | Vultr Docs
January 1, 2025 - In this article, you will learn how to deploy the numpy.zeros() function effectively in generating zero-filled arrays in Python. Explore how to create arrays of different shapes and dimensions and understand the use of data type specifications ...
🌐
Python.org
discuss.python.org › python help
Need help with a two-dimensional array - Python Help - Discussions on Python.org
June 2, 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.
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.zeros.html
numpy.zeros — NumPy v2.1 Manual
Return a new array of given shape filled with value. ... >>> np.zeros((2,), dtype=[('x', 'i4'), ('y', 'i4')]) # custom dtype array([(0, 0), (0, 0)], dtype=[('x', '<i4'), ('y', '<i4')])
🌐
Python Guides
pythonguides.com › python-numpy-2d-array
Create A 2D NumPy Array In Python (5 Simple Methods)
May 16, 2025 - import numpy as np # Create a 2D array filled with zeros zeros_array = np.zeros((3, 4)) # Create a 2D array filled with ones ones_array = np.ones((2, 5)) # Create a 2D array filled with a specific value filled_array = np.full((2, 3), 99) print("Zeros array:\n", zeros_array) print("\nOnes array:\n", ...
🌐
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: ...