PYTHON
Memory-Efficient Numerical Arrays with array.array
Discover how to use Python's `array.array` module for storing large sequences of homogeneous basic numeric types more memory-efficiently than standard lists.
import array
import sys
# Create an array of integers ('i' type code for signed int)
# Other common type codes: 'b' (signed char), 'B' (unsigned char), 'h' (signed short),
# 'H' (unsigned short), 'l' (signed long), 'L' (unsigned long), 'f' (float), 'd' (double)
my_array = array.array('i', [1, 2, 3, 4, 5])
print(f"Array: {my_array}")
print(f"Type code: {my_array.typecode}")
print(f"Item size (bytes): {my_array.itemsize}")
# Operations are similar to lists
my_array.append(6)
my_array.extend([7, 8, 9])
print(f"Modified array: {my_array}")
print(f"Element at index 2: {my_array[2]}")
# Compare memory usage with a standard list
list_data = list(range(10000))
array_data = array.array('i', range(10000))
print(f"
Memory usage for list of 10,000 integers: {sys.getsizeof(list_data)} bytes")
print(f"Memory usage for array.array of 10,000 integers: {sys.getsizeof(array_data)} bytes")
How it works: The `array` module provides a space-efficient way to store sequences of basic numeric types. Unlike Python lists, which store references to arbitrary objects, `array.array` stores elements as a compact C-style array of a single, specified type. This makes it particularly useful for large datasets where memory efficiency or performance during I/O operations (like reading/writing binary files) is crucial, offering significant memory savings compared to a standard list of the same numerical data.