Python does not have a single built-in keyword called array like Java or C. Instead, Python gives you three array-like tools depending on your job: list for general data, array.array for compact typed sequences, and NumPy ndarray for fast math.
For 90% of everyday scripting, a standard Python list is the right choice because it integrates everywhere with zero imports.
| Option | Mixed Types? | Memory Footprint | Best Use Case |
|---|---|---|---|
list |
Yes | Higher (object pointers) | General collections, API data |
array.array |
No (single C type) | Compact | Binary streams, dense numeric lists |
numpy.ndarray |
No (fixed dtype) | Very compact | Matrix math, ML, data science |
array ModuleWhen you store millions of uniform integers or floats without installing third-party libraries, array.array packs raw C values tightly in memory. It supports indexing, slicing, .append(), and .extend() just like a list, while enforcing a strict single-byte type code across every element.
from array import array
# 'i' = signed integer, 'd' = double-precision float
sensor_ids = array("i", [101, 102, 103])
sensor_ids.append(104)
temperatures = array("d", [21.5, 22.1, 20.8])
print(sensor_ids[1], sum(temperatures) / len(temperatures))
array.array: Appending "105" or 3.5 to an integer array("i") raises TypeError.numpy vectorized operations instead of Python for loops.list slicing returns a new list, whereas slicing a NumPy array returns a view sharing the same memory.Yes. Under the hood, CPython stores a list as a contiguous C array of pointers to Python objects, resizing automatically as you append items.
array.array?No. The array module is built directly into the Python standard library.
array.array?Common codes include 'i' (signed int), 'I' (unsigned int), 'f' (32-bit float), and 'd' (64-bit float).
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Experiment with the code from this lesson in our interactive playground.