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
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ reference โ€บ generated โ€บ numpy.zeros.html
numpy.zeros โ€” NumPy v2.5 Manual
>>> np.zeros((2,), dtype=[('x', 'i4'), ('y', 'i4')]) # custom dtype array([(0, 0), (0, 0)], dtype=[('x', '<i4'), ('y', '<i4')])
๐ŸŒ
NumPy
numpy.org โ€บ devdocs โ€บ reference โ€บ generated โ€บ numpy.zeros.html
numpy.zeros โ€” NumPy v2.6.dev0 Manual
Return an array of zeros with shape and type of input. ... Return a new uninitialized array. ... Return a new array setting values to one. ... Return a new array of given shape filled with value. ... Try it in your browser! >>> import numpy as np >>> np.zeros(5) array([ 0., 0., 0., 0., 0.])
๐ŸŒ
w3resource
w3resource.com โ€บ numpy โ€บ array-creation โ€บ zeros.php
NumPy: numpy.zeros() function - w3resource
April 21, 2026 - In this example, a tuple (3, 2) defines the shape of the array, and np.zeros() creates a 2D array filled with zeros.
๐ŸŒ
DataCamp
datacamp.com โ€บ doc โ€บ numpy โ€บ zeros
NumPy zeros()
import numpy as np array_1d = np.zeros(5)Powered by ยท This creates a one-dimensional array with 5 elements, all initialized to zero. array_2d = np.zeros((3, 4), dtype=int)Powered by ยท Here, a 3x4 two-dimensional array of integers is created, all initialized to zero.
๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ python:numpy โ€บ built-in functions โ€บ .zeros()
Python:NumPy | Built-in Functions | .zeros() | Codecademy
June 23, 2025 - For example, (3, 2) creates a 2D ... in row-major ('C', default) or column-major ('F') order. ... Returns a new array of the given shape, filled entirely with zeros....
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ numpy โ€บ numpy-zeros-python
numpy.zeros() in Python - GeeksforGeeks
June 15, 2026 - Explanation: np.zeros((3, 4)) creates a 2D array with 3 rows and 4 columns filled with 0.
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.2 โ€บ reference โ€บ generated โ€บ numpy.zeros.html
numpy.zeros โ€” NumPy v2.2 Manual
Return an array of zeros with shape and type of input. ... Return a new uninitialized array. ... Return a new array setting values to one. ... 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')])
Find elsewhere
๐ŸŒ
iO Flood
ioflood.com โ€บ blog โ€บ np-zeros
NP.Zeroes | Using Zeros() In Numpy
January 31, 2024 - To create a 2D array, we pass a tuple to the np.zeros function, specifying the number of rows and columns.
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ python โ€บ third party โ€บ numpy โ€บ zeros()
Python Numpy zeros() - Create Zero Array | Vultr Docs
January 1, 2025 - Create a 2D or 3D zero array using this shape specification. ... two_d_array = np.zeros((2, 3)) print(two_d_array) three_d_array = np.zeros((2, 3, 4)) print(three_d_array)
๐ŸŒ
AskPython
askpython.com โ€บ python-modules โ€บ numpy โ€บ numpy-zeros-method-in-python
NumPy zeros() Method in Python - AskPython
March 12, 2020 - As we can see from the output, we get a 2D array with all of the elements as 0. We can also create arrays with heterogeneous data type using the zeros() function in Python. We just need to pass a tuple containing the required information. import ...
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.1 โ€บ reference โ€บ generated โ€บ numpy.zeros.html
numpy.zeros โ€” NumPy v2.1 Manual
Return an array of zeros with shape and type of input. ... Return a new uninitialized array. ... Return a new array setting values to one. ... 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')])
๐ŸŒ
CASRAI
casrai.org โ€บ guides โ€บ research tools & software โ€บ statistical software for research โ€บ numpy zeros: np.zeros() syntax and examples
NumPy Zeros: np.zeros() Syntax and Examples โ€” CASRAI
2 weeks ago - NumPy zeros explained: np.zeros() shape, dtype and order, 1D and 2D examples, integer zeros, and when to use np.empty, ones or full instead.
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.3 โ€บ reference โ€บ generated โ€บ numpy.zeros.html
numpy.zeros โ€” NumPy v2.3 Manual
>>> np.zeros((2,), dtype=[('x', 'i4'), ('y', 'i4')]) # custom dtype array([(0, 0), (0, 0)], dtype=[('x', '<i4'), ('y', '<i4')])
๐ŸŒ
NumPy
numpy.org โ€บ devdocs โ€บ reference โ€บ generated โ€บ numpy.zeros_like.html
numpy.zeros_like โ€” NumPy v2.6.dev0 Manual
Return a new array setting values to zero. ... Try it in your browser! >>> import numpy as np >>> x = np.arange(6) >>> x = x.reshape((2, 3)) >>> x array([[0, 1, 2], [3, 4, 5]]) >>> np.zeros_like(x) array([[0, 0, 0], [0, 0, 0]])
๐ŸŒ
Medium
medium.com โ€บ @debopamdeycse19 โ€บ what-is-the-use-of-the-zeros-function-in-numpy-arrays-in-python-dde91b18e234
What is the use of the Zeros function in numpy arrays in Python? | by Let's Decode | Medium
November 28, 2023 - # Creating a 2x3 array (2 rows, 3 columns) filled with zeros import numpy as np arr = np.zeros((2, 3)) print(arr) ... Note: For a 2D array, you would provide the shape as a tuple with two values: (rows, columns).
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ python โ€บ define a two-dimensional array in python
Python Define a Two-Dimensional Array or Matrix | Sentry
September 23, 2026 - Create a 2D array in Python using numpy.zeros for matrix operations, or a nested list comprehension if you want to avoid the numpy dependency
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.0 โ€บ reference โ€บ generated โ€บ numpy.zeros.html
numpy.zeros โ€” NumPy v2.0 Manual
Return an array of zeros with shape and type of input. ... Return a new uninitialized array. ... Return a new array setting values to one. ... 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')])