Back to Study Material Python

Arrays

Python does not have a built-in fixed-type array like C or Java. Python lists serve as general-purpose dynamic arrays but store references to objects, whic...

Arrays

Python Lists vs Arrays

Python does not have a built-in fixed-type array like C or Java. Python lists serve as general-purpose dynamic arrays but store references to objects, which means each element can be a different type. The array module in the standard library provides typed, compact arrays: import array; a = array.array("i", [1, 2, 3, 4]) creates an array of C-style integers. The type code "i" specifies the element type. For numerical computing, NumPy arrays (ndarray) are far more commonly used.

The array Module

The array.array type stores data in a compact, typed format. Common type codes: "i" for signed int, "f" for float, "d" for double, "b" for signed char. Operations are similar to lists: a.append(5), a.extend([6,7]), a.pop(), and len(a). array objects are more memory-efficient than lists for large collections of numbers because they do not store per-element Python object overhead. They also support reading from and writing to binary files with fromfile() and tofile().

NumPy Arrays Overview

For numerical work, NumPy's ndarray is the standard array type in Python. import numpy as np; a = np.array([1, 2, 3, 4]). NumPy arrays are fixed-type, homogeneous, and support vectorised operations: a * 2 gives array([2, 4, 6, 8]) without a loop. They can be multi-dimensional: np.zeros((3, 4)) creates a 3x4 matrix of zeros. NumPy is covered in detail in the Data Science section, but knowing when to choose it over a list or array.array is important.

Choosing the Right Sequence Type

Use a Python list when you need a general-purpose, mixed-type sequence with frequent additions and deletions. Use array.array when you need a memory-efficient sequence of homogeneous numeric data and do not need NumPy. Use a NumPy ndarray when you need fast vectorised numerical operations, linear algebra, or multi-dimensional arrays. Use a deque (from collections) when you need fast insertions and deletions at both ends. Each type has its ideal use case.

Worked Example: array vs list

Storing one million floats: a list requires roughly 8MB for references plus 28 bytes per float object. An array.array("d", data) requires about 8MB total because each double is 8 bytes with no object overhead. import array, sys; lst = list(range(1000000)); arr = array.array("i", range(1000000)); sys.getsizeof(arr) is much smaller than sys.getsizeof(lst). For programs processing large numerical datasets without NumPy, array.array provides meaningful memory savings.

Exam Tip

Exam questions about Python arrays often test whether you know that Python's default list is not a fixed-type array. Know that array.array is in the standard library (import array) and that NumPy's ndarray is a third-party package. A common question: what advantage does array.array have over a list for numbers? Memory efficiency — it stores raw data values rather than Python object references. Also know that all elements in an array.array must be of the same type; attempting to add an incompatible type raises TypeError.