Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix 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 desk3 min

How to Initialize an Array in Python

Initialize a Python list with a literal, use array.array for typed numeric values, or create a NumPy ndarray for numerical and multidimensional work.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For a general-purpose sequence, initialize a Python list: values = [1, 2, 3]. Python also has a typed standard-library array and NumPy arrays, which are useful for numeric data and multidimensional shapes. Choose the type that matches what you need; “array” can mean more than one thing in Python.

Choose the right Python array type

Use case Choose Example
General-purpose sequence of Python objects Built-in list [1, 2, 3]
Typed numeric values without NumPy Standard-library array.array array('i', [1, 2, 3])
Numerical operations or rectangular multidimensional data NumPy ndarray np.array([[1, 2], [3, 4]])

Python’s Data Structures tutorial describes list literals and list operations. The Python 3.14 array reference documents typed numeric arrays. NumPy’s Array creation guide and beginner’s guide cover ndarray creation and behavior.

Initialize a list for ordinary Python code

A list is usually the right answer when you want a sequence that can hold general Python objects. It can contain numbers, strings, or values of different types.

values = [1, 2, 3]
empty = []
zeros = [0] * 5

Use a list comprehension when each initial value is computed separately:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
values = [make_value(i) for i in range(5)]

For a two-dimensional list whose rows should be independent, create each row with a comprehension:

row_count = 3
columns = 4
rows = [[0] * columns for _ in range(row_count)]

Avoid [[0] * columns] * row_count when you intend independent rows: that expression repeats references to the same inner list, so changing one row changes them all.

Create a typed numeric array with array.array

The standard-library array module stores numeric values using a specified type code. It is distinct from both a list and NumPy’s multidimensional ndarray.

from array import array

values = array('i', [1, 2, 3])
empty_ints = array('i')

The type code, such as 'i' in this example, determines the element type. This is a one-dimensional typed array; use NumPy if your task calls for multidimensional numerical operations.

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

Create a NumPy array from existing values

Use np.array() to turn a sequence into a NumPy array. Rectangular nested sequences create arrays with multiple dimensions:

import numpy as np

from_values = np.array([1, 2, 3])
from_nested_values = np.array([[1, 2], [3, 4]])

NumPy arrays are generally homogeneous and have a fixed total size after creation. Nested input should have a rectangular shape, and you can set dtype when the element type matters:

values = np.array([1, 2, 3], dtype=np.int32)

Initialize by shape with zeros, ones, or empty

When you know the dimensions but not the values, use a shape-based constructor. Specify the fill type when you need something other than the default floating-point type.

zeros = np.zeros((2, 3), dtype=int)
ones = np.ones((2, 3), dtype=np.float32)

np.zeros() defaults to float64; it creates integer zeros here because dtype=int is specified. np.ones() accepts a shape and dtype in the same way.

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

np.empty() allocates an array without initializing its elements to a known value. Its contents are not guaranteed to be zero, so assign every element before reading it:

result = np.empty((2, 3), dtype=int)
result[:] = 0
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Choose between arange and linspace

For a sequence defined by increments, use np.arange(). Prefer integer start, stop, and step values when possible:

indexes = np.arange(0, 10, 2)  # 0, 2, 4, 6, 8

For an exact number of evenly spaced points between endpoints, use np.linspace():

samples = np.linspace(0, 1, 5)  # five values, including both endpoints

Floating-point increments with arange() can have rounding and endpoint subtleties. Use linspace() when the point count and endpoints are what matter.

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.

Quick answer: how do I create an empty array in Python?

Choose what “empty” means for your task:

  • empty = [] creates an empty, general-purpose list.
  • empty_ints = array('i') creates an empty typed standard-library array.
  • empty = np.empty((2, 3)) allocates a NumPy array with a shape but uninitialized contents; fill it before reading.

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 *

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

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.