← Back to Python Course | Chapter 12: Standard Library | Lesson 9 of 9

Python array Module

The array module makes a list that holds only one kind of number, like an egg carton with slots that fit only eggs. It uses less memory than a regular list.
Syntax
python
from array import array

array_name = array("type_code", [item1, item2])

What Is the array Module?

The built-in array module provides a compact, type-constrained sequence, unlike Python's regular list which can freely mix integers, strings, and objects in a single container.

Every element in an array.array must share the same declared type, which lets it store data more compactly in memory.

Example: What Is the array Module?

python
import array
arr = array.array("i", [1, 2, 3])  # "i" means every element is a signed int
print(arr)

Creating a Typed Array

You create one with array.array(typecode, values), where the typecode is a single character like i for signed int or d for double-precision float, declared up front and enforced on every insertion.

Attempting to add a value of the wrong type raises a TypeError immediately, unlike a list which would silently accept it.

Example: Creating a Typed Array

python
import array
arr = array.array("d", [1.5, 2.5])  # "d" means every element is a double-precision float
try:
    arr.append("not a float")  # wrong type, rejected immediately
except TypeError as e:
    print("Error:", e)

array vs list

A plain list is more flexible and is almost always the right default choice, but an array uses noticeably less memory when storing a large number of same-typed numeric values because it avoids the overhead of storing full Python objects for each element.

Reach for it in memory-constrained numeric-heavy code, not as a general replacement for list.

Example: array vs list

python
import array
import sys
arr = array.array("i", [1, 2, 3])
lst = [1, 2, 3]
print(sys.getsizeof(arr) < sys.getsizeof(lst))  # array uses less memory for same-typed numbers

array vs NumPy

This built-in array module is not the same as NumPy's ndarray, which is covered elsewhere on this site — NumPy is a separate third-party library offering vectorized math operations, multi-dimensional shapes, and broadcasting that the standard-library array module doesn't provide at all.

array is a lightweight stdlib tool, NumPy is a full numerical computing library.

Example: array vs NumPy

python
import array
arr = array.array("i", [1, 2, 3])
print(type(arr))
# NumPy's ndarray adds vectorized math and multi-dimensional shapes on top of this

Common array Operations

Arrays support most list-like operations — indexing, slicing, append(), iteration — plus a few array-specific ones like tobytes() for converting to a raw byte representation, useful when interfacing with binary file formats or C-level APIs that expect packed numeric data.

Example: Common array Operations

python
import array
arr = array.array("i", [1, 2, 3])
arr.append(4)  # list-like operation
print(arr[1:3])  # slicing works like a list
print(arr.tobytes())  # raw byte representation for binary interop
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using array with the wrong type code, such as "i" for a float, which raises TypeError.
  2. Assuming an array can hold mixed types like a list, when all elements must match the type code.
  3. Expecting array maths like NumPy, when array("i", [1, 2]) * 2 repeats the array instead of doubling values.
Chapter Summary
  • The os, sys, and pathlib modules work with the system and file paths.
  • math, random, datetime, and re support common calculations, dates, and text patterns.
  • argparse and array handle command-line arguments and arrays.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.