🚀 Supercharge your YouTube channel's growth with AI.
Try YTGrowAI Free3 Ways to Initialize an Array in Python

Some calculations start with a known number of values that need initial contents. Python provides ways to create that set, from a general-purpose list to a numerical array with a chosen type or shape.
I’ll show how to choose among these options and set starting values for a NumPy row or grid.
TL;DR
Use a Python list for general values, array.array for compact typed values, or NumPy when you need shaped numerical data.
- Use [value] * size for a one-dimensional list with a repeated immutable value.
- Use array(typecode, values) when a standard-library sequence must enforce a numeric type.
- Use np.zeros(shape) or np.full(shape, value) for NumPy arrays whose initial contents matter.
- Use np.empty(shape) only when every numeric entry will be assigned before it is read.
What Does “Array” Mean in Python?
Python code uses “array” for three related but distinct containers: a built-in list, the standard-library array.array, and a NumPy ndarray. Their element types and dimensions differ.
A list is a mutable sequence that can hold objects of different types and can grow as values are appended.
array.array is also a mutable sequence, but its typecode restricts stored values to a supported C-style type. The Python array documentation describes its compact numeric representation and initializer. Its item size depends on the platform, so inspect itemsize when the byte width matters.
A list comprehension builds values one by one. NumPy creation functions expose shape and dtype directly.
The output from the run makes the shape difference visible: the NumPy result has two rows and three columns. Its dtype is shown separately.

These distinctions matter when another function expects a particular container. A library may accept a Python sequence and convert it, while NumPy operations return ndarrays. Check the returned type and shape at the boundary when your next operation depends on them.
list: [7, 7, 7, 7]
list type: list
typed array: [7, 7, 7, 7]
typecode: i
NumPy shape: (2, 3)
NumPy dtype: int32
NumPy values: [[7, 7, 7], [7, 7, 7]]
How to Initialize an Array in Python
Choose the constructor that matches the values and shape you need. Each example below shows a distinct way to create the result.
Step 1: Fill a Python list with a repeated value
Multiply a one-element list by the requested length to create repeated references to that element. For immutable values such as integers, strings, or None, this initializes a one-dimensional sequence.
scores = [0] * 5
labels = ["pending"] * 3
print(scores)
print(labels)
The five zero values and three labels are created immediately. A list comprehension is another clear option when each position needs a calculated value rather than the same constant.
squares = [number * number for number in range(5)]
A list has no fixed capacity contract. The multiplication expression creates the requested initial number of elements, and later append operations can change the length. If the number of entries is calculated at runtime, keep that calculation separate so the intended size is clear and can be checked.
size = 4
initial = [None] * size
initial[0] = "ready"
print(len(initial))
print(initial)
This creates four positions, then replaces the first value with a string. Use a distinct sentinel when the application must tell an unfilled slot from valid data.
A comprehension can also calculate each element from its index. It makes the size and value rule explicit in one expression.
positions = [index * 10 for index in range(4)]
Use repetition for a constant initial value, and a comprehension when each slot depends on its index or another expression. Both return a list, so later list operations work the same way.
Step 2: Build a typed sequence with array.array
Import array from the standard library and give its constructor a typecode plus an iterable of initial values. This keeps the result a typed sequence without adding a third-party dependency.
from array import array
measurements = array("i", [0] * 4)
measurements.append(12)
print(measurements.tolist())
print(measurements.typecode)
The typecode i requests a signed integer type. Python documents accepted codes and minimum sizes. Actual item sizes can vary by platform, so inspect the itemsize attribute when your interface depends on storage width.
array.array is a fit when a homogeneous numeric sequence and the standard library meet the requirement. Its typecode restricts values to the selected representation.
Step 3: Initialize one- or two-dimensional NumPy arrays
NumPy provides shape and dtype controls for numerical calculations across one or more dimensions. Choose an explicit dtype when later operations require a specific representation.
import numpy as np
one_dimensional = np.zeros(4, dtype=np.int32)
grid = np.full((2, 3), 7, dtype=np.int32)
print(one_dimensional)
print(grid)
print("shape:", grid.shape)
print("dtype:", grid.dtype)
Use np.zeros() when zero is the intended starting value. If zero could be mistaken for a measurement, choose a sentinel or keep missingness separately.
The NumPy dtype controls how each entry is represented. Without an explicit dtype, zeros produces floating-point values, which print with decimal points. An integer dtype matches calculations that expect whole-number values.
For a constant other than zero, np.full() takes the requested shape and fill value. It can represent a starting baseline, a sentinel supported by the chosen dtype, or text labels. The type must be able to represent the fill value, so select dtype with the later values in mind.
A NumPy shape lists the length of each axis. For example, (3, 2) requests three rows and two columns, while (2, 3) requests two rows and three columns.
The element count is the product of those dimensions, so a reshaped sequence must contain the matching number of values. A table-shaped result is useful when each row represents one record and each column represents one measurement.
When an existing array already defines the desired dimensions, NumPy also provides “like” creation functions such as np.zeros_like(). They inherit shape and dtype from another array unless you override an option. This is useful when a new result must align with a known input rather than a separately typed size.
NumPy’s creation routines also include np.ones() for ones and np.arange() for a generated numeric range. The former sets every element to one. The latter creates values from a start, stop, and optional step, which can supply initial values when a formula is more useful than a constant fill.
A zero-dimension shape has no elements. By contrast, np.empty() with a nonzero shape allocates entries without assigning numeric values.
When filling the array with a range, generate values with np.arange() and reshape only when the element count matches the target dimensions. For example, six values fit shape (2, 3). See AskPython’s multidimensional arrays guide for more operations on nested shapes.
What Can Go Wrong When Initializing Arrays?
Before using an initialized array, check its length and fill value, then confirm that nested rows do not share an inner list by accident.
A zero-element list is different from a list with empty slots
The list length is zero, while slots has four entries. Check the length when later code depends on positions already existing.
blank = []
slots = [None] * 4
print(len(blank), len(slots))
Length counts existing elements, not whether their values are ready for a calculation. Apply the same check to NumPy shapes with a zero dimension and to numeric arrays created with np.empty().
Repeated nested lists share one inner list
Repeated nested rows can alias the same inner list, so the code below compares mutation behavior with independently built rows.
shared = [[0] * 3] * 2
shared[0][0] = 9
independent = [[0] * 3 for _ in range(2)]
independent[0][0] = 9
print(shared)
print(independent)
Build rows separately when edits to one row must not affect another.
NumPy empty does not mean zero-filled
np.empty(shape) allocates an array without setting numeric entries to known values. Those entries may hold arbitrary data until you assign them, so read them only after your code has filled every position.
NumPy documents an exception for object arrays: their entries are initialized to None. That behavior does not make empty a general way to create a zero-filled numeric array. Use np.zeros() when zeros are the intended starting values.
import numpy as np
values = np.empty((2, 3), dtype=np.int32)
values[:] = [[1, 2, 3], [4, 5, 6]]
print(values)
Here every entry is assigned before printing. The NumPy empty reference cautions that numeric values are arbitrary before assignment. Use AskPython’s empty and empty_like guide for related allocation choices.
For list-like rows with different lengths, retain a list of lists if each row has its own meaning. A regular numeric NumPy matrix requires a rectangular shape, so decide how unequal rows should be represented before creating one.
Conclusion: Choose by the Data You Store
Choose the container by its behavior: the list handles general objects, array.array enforces a typecode, and NumPy provides regular numerical shapes. Match the constructor to the values your next step will read.
For follow-up reading, see the Python array documentation, NumPy’s array creation routines, and AskPython’s guides to NumPy zeros and NumPy arrays.
FAQ
These answers separate Python sequences from NumPy arrays and their storage rules.
How do I initialize an array in Python with a size?
For a general one-dimensional sequence, use a list such as [None] * size or [0] * size. For a NumPy array, pass a length or shape tuple to a creation function such as np.zeros() or np.full().
How do I initialize a 2D NumPy array with zeros?
Pass a tuple containing the row and column counts to np.zeros(), such as np.zeros((2, 3)). Specify dtype when the default floating-point type is not what you want.
Does np.empty() create an array of zeros?
No. Numeric entries from np.empty() are uninitialized and may contain arbitrary values. Assign them before reading, or use np.zeros() when zero values are required.


