How to Use Array Typecodes for Compact Numeric Storage in Python
This code demonstrates how to use the `array` module with typecodes to store integers, floats, and bytes in a memory-efficient way compared to standard Python lists.
Python code
31 linesfrom array import array
def demonstrate_array_types():
# Compact integer arrays
small_ints = array('i', [1, 2, 3, 4, 5])
unsigned_ints = array('I', [10, 20, 30])
# Floating point arrays
floats = array('f', [1.5, 2.5, 3.5])
doubles = array('d', [1.123456789, 2.987654321])
# Character array
chars = array('b', [65, 66, 67]) # ASCII for A, B, C
# Display type codes and sizes
print(f"Signed int ('i'): {small_ints.tolist()}, itemsize={small_ints.itemsize} bytes")
print(f"Unsigned int ('I'): {unsigned_ints.tolist()}, itemsize={unsigned_ints.itemsize} bytes")
print(f"Float ('f'): {floats.tolist()}, itemsize={floats.itemsize} bytes")
print(f"Double ('d'): {doubles.tolist()}, itemsize={doubles.itemsize} bytes")
print(f"Byte ('b'): {[chr(c) for c in chars]}, itemsize={chars.itemsize} byte")
# Memory efficiency demo
import sys
py_list = [1, 2, 3, 4, 5]
py_bytes = sys.getsizeof(py_list) + sum(sys.getsizeof(x) for x in py_list)
arr_bytes = sys.getsizeof(small_ints) + small_ints.itemsize * len(small_ints)
print(f"\nPython list size: {py_bytes} bytes")
print(f"Compact array size: {arr_bytes} bytes")
if __name__ == "__main__":
demonstrate_array_types()
Output
Signed int ('i'): [1, 2, 3, 4, 5], itemsize=4 bytes
Unsigned int ('I'): [10, 20, 30], itemsize=4 bytes
Float ('f'): [1.5, 2.5, 3.5], itemsize=4 bytes
Double ('d'): [1.123456789, 2.987654321], itemsize=8 bytes
Byte ('b'): ['A', 'B', 'C'], itemsize=1 byte
Python list size: 196 bytes
Compact array size: 44 bytes
How it works
The array module provides a data structure that stores homogeneous values in a compact C-style format, governed by a single typecode character. Each typecode maps to a specific C type with a fixed byte size, such as 'i' for a 4-byte signed integer or 'd' for an 8-byte double. This makes arrays significantly more memory-efficient than lists, which store references to full Python objects with additional overhead. Using .tolist() converts the array to a standard list for display, while .itemsize reveals the size of each element. When memory usage is a priority, arrays are a simple drop-in alternative to lists for large numeric datasets.
Common mistakes
- Forgetting that a typecode like 'i' is platform-dependent and may be 2 or 4 bytes on some systems
- Assuming you can store mixed types (e.g., ints and floats) in a single array — they must be homogeneous
- Overlooking that array elements are C-typed values, so some operations behave differently than list operations
Variations
- Use typecode 'l' or 'L' for 8-byte signed/unsigned integers on most platforms
- Use memoryview or numpy for even more specialized, high-performance numeric handling
Real-world use cases
- Storing millions of sensor readings in memory within an IoT data-processing pipeline.
- Buffering raw binary data from network sockets or file reads before decoding.
- Persisting numeric datasets to disk with array.tofile() for fast serialization.
Sponsored
More from Concurrency & performance
- Benchmark list.append vs deque.append in Python medium
- Build a Python Performance Profiler That Generates Readable Reports medium
- Graceful Shutdown Executor Context Manager in Python medium
- How to Build a Producer-Consumer Pattern with asyncio.Queue in Python medium
- How to Cancel an asyncio Task with Graceful Cleanup in Python medium
- How to Convert Data in Parallel with ThreadPoolExecutor in Python easy
Keep learning
Related tutorials and quizzes for this topic.