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

How to declare array of zeros in python (or an array of a certain size) - Stack Overflow
I am trying to build a histogram of counts... so I create buckets. I know I could just go through and append a bunch of zeros i.e something along these lines: buckets = [] for i in xrange(0,100): ... More on stackoverflow.com
🌐 stackoverflow.com
How to initialize a two-dimensional array (list of lists, if not using NumPy) in Python? - Stack Overflow
I'm beginning python and I'm trying to use a two-dimensional list, that I initially fill up with the same variable in every place. I came up with this: def initialize_twodlist(foo): twod_list ... More on stackoverflow.com
🌐 stackoverflow.com
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
4
0
June 3, 2022
Fill array with zeros like numpy does?
This is wrong because each row references the same array. If you change one value in the array, it will be changed for all rows. You should put the new double[3] statement directly inside the loop. Alternatively, if you're dealing with matrices, you should use n-dimensional arrays instead: - var bigDaddy = new double[1001, 3] this will create a 1001x3 matrix filled with zeroes by default - var bigDaddy = new double[3003] this will create a 3003 sized array filled with zeroes which you can then navigate with the % operator and use Span for operations for a performance boost More on reddit.com
🌐 r/csharp
20
0
October 31, 2024
🌐
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....
🌐
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.
🌐
Codecademy
codecademy.com › docs › python:numpy › built-in functions › .zeros()
Python:NumPy | Built-in Functions | .zeros() | Codecademy
June 23, 2025 - In this codebyte example, .zeros() is used to initialize a 2D array to store temperature sensor readings for 7 days across 3 different locations: ... Here, .zeros() prepares a placeholder matrix to store daily sensor readings, which will be ...
🌐
W3docs
w3docs.com › home › code snippets › python › how to initialize a two-dimensional array in python?
How to initialize a two-dimensional array in Python? | W3docs
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:
🌐
Python Guides
pythonguides.com › initialize-a-2d-array-in-python
How to Initialize a 2D Array in Python? (2026 Guide)
September 22, 2026 - NumPy is a third-party library for numeric computing in Python. It is not part of the standard library, so you need to install it first with pip install numpy. If it is already on your machine, these functions build 2D arrays fast, and they avoid the shared-reference trap entirely. Learn more in this guide to the NumPy 2D array. import numpy as np zeros_grid = np.zeros((10, 5)) print(zeros_grid)
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python › initialize-matrix-in-python
Initialize Matrix in Python - GeeksforGeeks
July 11, 2025 - For each row, [0]*cols creates a new list with two zeros by multiplying the value 0 by the number of columns. Generates a fast, memory-efficient 2D array of zeros using NumPy, ideal for numeric data and large matrices.
🌐
GeeksforGeeks
geeksforgeeks.org › numpy › numpy-zeros-python
numpy.zeros() in Python - GeeksforGeeks
June 15, 2026 - All elements are initialized to zero. ... Explanation: np.zeros((3, 4)) creates a 2D array with 3 rows and 4 columns filled with 0.
🌐
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.
🌐
w3resource
w3resource.com › numpy › array-creation › zeros.php
NumPy: numpy.zeros() function - w3resource
April 21, 2026 - ... 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.
🌐
DataCamp
datacamp.com › doc › numpy › zeros
NumPy zeros()
The `numpy.zeros` function is employed when you need an array of a specific shape initialized with zeros, often as placeholders before performing computations.
🌐
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, ...
🌐
MLJAR
mljar.com › answers › define-two-dimensional-array-python
Define two-dimensional array in Python
array[2][2] = 5 # valid array[7][2] = 3 # error · This way is much easier. You can use zeros function from numpy to create 2D array with all values set to zero: import numpy array = numpy.zeros((4,4)) REMEMBER Even though the second version is easier, the first one is better for this operation ...
🌐
Python.org
discuss.python.org › python help › need help with a two-dimensional array
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.
🌐
Python Guides
pythonguides.com › create-an-array-of-zeros-in-python
How To Create An Array Of Zeros In Python?
July 7, 2026 - How does this code work? You pass (12, 3) as the shape and np.int64 as the dtype to np.zeros(), which creates a 2D array of zeros where every element is a 64‑bit integer.
🌐
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 ...
🌐
Cf
physics-python.astro.cf.ac.uk › Week6
PX1224 -Week6: Two Dimensional Arrays
A 2D array can be "sliced" to return a 1D array containing one row or column of the original. So, for example, the zeroth (Python starts array with index 0) row or column is given by