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 › devdocs › reference › generated › numpy.zeros.html
numpy.zeros — NumPy v2.6.dev0 Manual
Return a new array of given shape and type, filled with zeros. ... Shape of the new array, e.g., (2, 3) or 2. ... The desired data-type for the array, e.g., numpy.int8. Default is numpy.float64. order{‘C’, ‘F’}, optional, default: ‘C’ · Whether to store multi-dimensional data in row-major (C-style) or column-major (Fortran-style) order in memory. ... The device on which to place the created array.
🌐
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.
🌐
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 › stable › reference › generated › numpy.zeros.html
numpy.zeros — NumPy v2.5 Manual
Return a new array of given shape and type, filled with zeros. ... Shape of the new array, e.g., (2, 3) or 2. ... The desired data-type for the array, e.g., numpy.int8. Default is numpy.float64. order{‘C’, ‘F’}, optional, default: ‘C’ · Whether to store multi-dimensional data in row-major (C-style) or column-major (Fortran-style) order in memory. ... The device on which to place the created array.
🌐
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 array with 3 rows and 2 columns. dtype: The desired data type for the array elements (default is float). order: Whether to store the array 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 › python › create-a-numpy-array-filled-with-all-zeros-python
Create a Numpy array filled with all zeros - Python - GeeksforGeeks
September 19, 2025 - We can use Numpy.zeros() method to do this task. ... By default, the data type of the array is float, so the zeros are represented as 0.0. ... This code creates a 2D array of zeros with 3 rows and 4 columns using np.zeros().
🌐
Python Examples
pythonexamples.org › python-numpy-zeros
Create Array with Zeros in NumPy - Examples
To create a numpy array with all zeros, of specific dimensions or shape, use numpy.zeros() function.
Find elsewhere
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.zeros.html
numpy.zeros — NumPy v2.2 Manual
Return a new array of given shape and type, filled with zeros. ... Shape of the new array, e.g., (2, 3) or 2. ... The desired data-type for the array, e.g., numpy.int8. Default is numpy.float64. order{‘C’, ‘F’}, optional, default: ‘C’ · Whether to store multi-dimensional data in row-major (C-style) or column-major (Fortran-style) order in memory. ... Reference object to allow the creation of arrays which are not NumPy arrays.
🌐
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 - The following code will create a 3-by-3 2D array with all values set to zero: import numpy matrix = numpy.zeros((3,3)) # array([[0., 0., 0.], # [0., 0., 0.], # [0., 0., 0.]])
🌐
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 - numpy.zeros() creates an array of zeros with any shape and supports multi-dimensional arrays. It’s very fast and memory-efficient, ideal for large numeric datasets and scientific computing.
🌐
NumPy
numpy.org › doc › stable › user › basics.creation.html
Array creation — NumPy v2.5 Manual
The ndarray creation functions can create arrays with any dimension by specifying how many dimensions and length along that dimension in a tuple or list. numpy.zeros will create an array filled with 0 values with the specified shape.
🌐
datagy
datagy.io › home › numpy › numpy zeros: create zero arrays and matrix in numpy
NumPy Zeros: Create Zero Arrays and Matrix in NumPy • datagy
December 30, 2022 - NumPy makes it equally easy to create a 2-dimensional zero matrix. We can do this by passing in a tuple of integers that represent the dimensions of this matrix. By default, NumPy will use a data type of floats.
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.zeros.html
numpy.zeros — NumPy v2.1 Manual
Return a new array of given shape and type, filled with zeros. ... Shape of the new array, e.g., (2, 3) or 2. ... The desired data-type for the array, e.g., numpy.int8. Default is numpy.float64. order{‘C’, ‘F’}, optional, default: ‘C’ · Whether to store multi-dimensional data in row-major (C-style) or column-major (Fortran-style) order in memory. ... Reference object to allow the creation of arrays which are not NumPy arrays.
🌐
DataCamp
datacamp.com › doc › numpy › zeros
NumPy zeros()
`numpy.zeros` is a function in the NumPy library that creates a new array of given shape and type, filled with zeros.
🌐
AskPython
askpython.com › python-modules › numpy › numpy-zeros-method-in-python
NumPy zeros() Method in Python - AskPython
March 12, 2020 - We pass a tuple of integers to the Numpy zeros() method instead of a single one. This allows us to create a multi-dimensional array of int type(specified). As we can see from the output, we get a 2D array with all of the elements as 0.
🌐
iO Flood
ioflood.com › blog › np-zeros
NP.Zeroes | Using Zeros() In Numpy
January 31, 2024 - Have you ever found yourself in a situation where you needed to initialize an array with zeros in Python? If so, you’re in the right place. Just like a blank canvas waiting for an artist’s brush, the np.zeros function in Python’s numpy library allows you to create an array filled with zeros, ready for you to paint your data on it.
🌐
Vultr Docs
docs.vultr.com › python › third party › numpy › zeros()
Python Numpy zeros() - Create Zero Array | Vultr Docs
January 1, 2025 - Use numpy.zeros() to create an array. ... This code snippet creates a 1D array of length 5 filled with zeros. The output will be an array [0. 0. 0. 0. 0.]. Specify a shape as a tuple to generate multi-dimensional arrays.
🌐
Python Tutorial
pythontutorial.net › home › python numpy › numpy zeros()
NumPy zeros(): Creating an Array with a Shape & Filled with Zeros
August 16, 2022 - In layman’s terms, this example creates a 2-D array or a matrix that has two rows and three columns. By default, zeros() function uses the type float64 for its elements. For example: import numpy as np a = np.zeros((2, 3)) print(a.dtype)Copy