Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk2 min

Create an Array of Zeros in Python: 4 Easy Methods

Create zero-filled NumPy arrays, Python lists, or typed array.array sequences, and learn which method fits your code.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For a numerical array—especially one that needs multiple dimensions—use NumPy: np.zeros(5) creates five zeros in a NumPy ndarray. Python’s word “array” can also mean a built-in list or a typed array.array, so the right method depends on the type your code needs.

1. Create a NumPy array of zeros

Use numpy.zeros when your code expects a NumPy ndarray or needs NumPy’s numerical operations and multidimensional shapes. The function returns a new array filled with zeros. Its default element type is numpy.float64; specify dtype if you want integers or another type.

As an Amazon Associate I earn from qualifying purchases.

import numpy as np

zeros = np.zeros(5)                       # one-dimensional; float64 by default
integer_zeros = np.zeros(5, dtype=int)    # integer dtype
matrix = np.zeros((2, 3), dtype=int)      # two rows, three columns

A single number such as 5 defines a one-dimensional shape. Use a tuple such as (2, 3) for two dimensions. The optional order parameter controls C-style row-major or Fortran-style column-major memory layout. The device keyword was added in NumPy 2.0.0; if supplied for Array API interoperability, it must be "cpu". The like keyword, added in NumPy 1.20.0, can let a compatible array-like object handle array creation.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

See the NumPy zeros reference for the full signature and parameter details.

2. Make a simple Python list with repetition

For a flat sequence of zero values in ordinary Python code, repeat the integer zero:

n = 5
zeros = [0] * n

This returns a built-in list, not a NumPy ndarray. Repetition is appropriate here because 0 is immutable. Repeating a mutable object, such as a nested list, would repeat references to the same object.

3. Make a list with a comprehension

A list comprehension also returns a built-in list. It is useful when the value expression may need to become more involved:

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
n = 5
zeros = [0 for _ in range(n)]

For a two-dimensional nested list, construct a separate row on each iteration:

rows, cols = 2, 3
matrix = [[0 for _ in range(cols)] for _ in range(rows)]
# This is also safe for immutable zeros:
matrix = [[0] * cols for _ in range(rows)]

Avoid [[0] * cols] * rows if you plan to change individual rows. The outer repetition makes multiple references to the same inner list, so changing one row changes them all. Python’s built-in types documentation explains sequence repetition and list construction.

4. Use the standard-library array.array

For a mutable sequence of constrained basic numeric values without NumPy, use the standard-library array module:

from array import array

zeros = array('i', [0]) * 5

This returns an array.array, not a list or ndarray. The type code 'i' requests the C int type. The element representation and size depend on the machine architecture and C implementation, so this is not the same type system as NumPy’s dtypes. See Python’s array module documentation.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Which method should you choose?

Method Returned type Best fit
np.zeros(shape, dtype=...) NumPy ndarray NumPy operations, multidimensional numerical data, or code expecting an ndarray
[0] * n or a list comprehension Built-in Python list Simple Python sequence work
array('i', [0]) * n Standard-library array.array A sequence of constrained basic numeric values using a type code

Choose by the type expected by the code that will consume the values, then set the shape and element type. If integer elements matter in NumPy, pass dtype=int rather than relying on the default.

Why not use np.empty?

np.empty creates an array with uninitialized contents; it does not fill the elements with zeros. Use it only when every element will be assigned before being read. For a zero-initialization requirement, use np.zeros. NumPy’s user guide describes the uninitialized result.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.