Python Arrays
Tutorial 19 of 65 · pythondeck.com Python course
Standard Python lists already act like dynamic arrays. For homogeneous, compact numeric storage use array.array from the standard library; for fast numerical computing use numpy.ndarray.
Python has no built-in array type for arbitrary homogeneous numeric data in core tutorials—lists fill that role for general objects. The array.array module stores compact C-style buffers of primitive typecodes.
For serious numeric work, NumPy ndarray provides vectorized operations; knowing when to graduate from list to array saves memory and CPU. Tutorial wording often says array when it means list—clarify which type your code actually uses.
Lists as dynamic arrays: amortized append O(1), insert O(n) in middle.
import array; array.array('i', [1,2,3]) for typed binary storage.
Typecodes: 'i' int, 'f' float, 'd' double—see docs table.
NumPy: import numpy as np; a = np.array([1,2,3]) with shape, dtype, slicing.
bytes/bytearray for raw octet buffers, not floating-point math.
memoryview shares buffer protocol without copying large blobs.
array.array is niche; most apps use list until profiling shows memory pressure. NumPy broadcasting and ufuncs replace Python loops for large datasets.
Multidimensional data: nested lists work for teaching; NumPy or pandas for production tables.
NumPy slices may be views—copy when mutating; millions of floats belong in ndarray, not plain lists.
Expecting list += to work like vector addition without NumPy.
Using array.array with wrong typecode and truncating values.
Confusing Python list with fixed-size C array semantics.
Importing array module when you meant numpy and getting type errors.
Start with lists; introduce array.array or NumPy when requirements are clear.
Specify dtype in NumPy explicitly (dtype=np.float64) for reproducibility.
Measure before optimizing—lists are fine for thousands of elements in many scripts.
Document whether APIs return copies or views of underlying buffers.
When teaching, say list for general sequences and ndarray only when you import NumPy explicitly.
Re-read the examples below with these ideas in mind; change variable names and inputs to match your own project.
The program below demonstrates array module. Read the comments on each line, run the code, then change names or values to see how the output shifts.
# Example: array module
# Run in the REPL or save as a .py file and execute with python.
from array import array
a = array("i", [1, 2, 3, 4])
a.append(5)
print(a, a.itemsize, "bytes per element")
This sample walks through list as array in a small, runnable script. Paste it into the REPL or save it as a .py file before you continue to the next block.
# Example: List as array
# Run in the REPL or save as a .py file and execute with python.
row = [0] * 5
print(row)
grid = [[0]*3 for _ in range(3)]
print(grid)
Here is a hands-on illustration of numpy preview. Follow the inline comments first; only then execute the snippet and compare the result with what you expected.
# pip install numpy
import numpy as np
v = np.arange(10)
print(v * 2)
The program below demonstrates array typecode. Read the comments on each line, run the code, then change names or values to see how the output shifts.
# array.array stores homogeneous C types compactly
from array import array # stdlib typed array
ints = array("i", [1, 2, 3, 4]) # signed int typecode
print(ints.itemsize, ints.typecode) # bytes per item and code
ints.append(5) # grow sequence
print(ints.tolist()) # convert to Python list
buf = array("B", range(256)) # unsigned bytes 0..255
print(buf[0], buf[255]) # endpoints
view = memoryview(buf) # zero-copy buffer view
print(len(view), view[0]) # length and first byte
This sample walks through bytes buffer in a small, runnable script. Paste it into the REPL or save it as a .py file before you continue to the next block.
# bytes are immutable; bytearray is mutable binary data
raw = bytes([65, 66, 67]) # ABC as bytes
print(raw, raw.decode("ascii")) # b'ABC' ABC
mutable = bytearray(b"hello") # copy into bytearray
mutable[0] = ord("H") # in-place edit first byte
print(mutable.decode()) # Hello
mutable.extend(b" world") # append bytes
print(mutable.decode()) # Hello world
import struct # pack/unpack binary structs
packed = struct.pack(">I", 16909060) # big-endian uint32
print(packed.hex()) # hex representation