Explain Codes LogoExplain Codes Logo

How to declare array of zeros in python (or an array of a certain size)

python
list-comprehension
numpy
data-structures
Anton ShumikhinbyAnton Shumikhin·Feb 16, 2025
TLDR

One-dimensional list of ten zeros, short and sweet:

zeros_list = [0] * 10

Alternatively, an efficient NumPy array of zeros:

import numpy as np zeros_np = np.zeros(10)

Let's dive deeper to consider one-dimensional arrays, multidimensional arrays, performance, and limitations.

The problem with multidimensional arrays with list multiplication

Creating multidimensional arrays with list multiplication may introduce unexpected behavior due to shared references:

# The botched way botched_matrix = [[0] * 3] * 4 # Creates shared references!

Modifying one element in a sublist inadvertently affects the other sublists:

botched_matrix[0][0] = 1 # Alter ego matrix appears!

The right way with list comprehension

For multidimensional arrays, use list comprehension to avoid the issue of shared references:

# The right way right_matrix = [[0] * 3 for _ in range(4)] right_matrix[0][0] = 1 # Changes only the needed spot. Phew!

Using numpy for heavy-duty arrays

For larger, performance-critical arrays, NumPy's zeros comes to the rescue:

import numpy as np large_zeros_np = np.zeros((1000, 1000)) # Easily creates a 1000x1000 array

Deeper into list comprehension

List comprehension serves as a powerful tool for declaring zero arrays, handling dimensions flexibly and elegantly:

# A 3D array for the three-eyed ravens three_d_zeros_list = [[[0] for _ in range(3)] for _ in range(4) for _ in range(5)]

This allows for creating complex zero-filled data structures.

Custom function for versatile array creation

No numpy? No problem! Here's a custom function to create arrays of zeros of any shape:

def create_zeros(shape): if isinstance(shape, int): return [0] * shape return [create_zeros(sub_shape) for sub_shape in shape] # Create a custom 3x4 array of zeros custom_zeros = create_zeros([3, 4]) # Customizable and flexible, like a yoga instructor

Efficient histograms with numpy

Combining numpy.zeros with numpy.histogram is an efficient approach for working with histograms:

# Datasaurus Rex: Rawr! data = [ ... ] # Your data points here histogram, bins = np.histogram(data, bins=range(10)) filled_slots = np.zeros_like(bins)

This setup aids in performing complex data analysis and visualization.

Make your zero arrays work for you

Creating zero arrays with Python unpacking

You can mix zero arrays creation with Python's unpacking feature:

# Sharing is caring! first, *rest = [0] * 4 # first == 0, rest == [0, 0, 0]

Using array module for numeric arrays

For numeric data, Python’s array module provides a more memory-efficient alternative:

# Less is more! from array import array numeric_zeros = array('i', [0] * 10) # 'i' for integers

High precision arrays with numpy

When precision matters, consider specifying your data type, especially with floating-point numbers:

# Accuracy is the best policy! import numpy as np high_precision_zeros = np.zeros(10, dtype=np.float64)

The right data type adds to the accuracy of computations.