AI Skill Report Card
Using Python Array Module
Quick Start13 / 15
Pythonfrom array import array # Create an array of signed integers ('i' typecode) a = array('i', [1, 2, 3, 4, 5]) print(a) # array('i', [1, 2, 3, 4, 5]) print(a[2]) # 3 print(a.tolist()) # [1, 2, 3, 4, 5]
Choose array over list when storing millions of numbers of the same type and memory footprint matters, or when you need to pass raw numeric buffers to C libraries, files, or sockets.
Recommendation▾
Add a concrete 'bad outcome' example (e.g., showing the actual TypeError or OverflowError output) rather than only describing pitfalls in prose
Workflow13 / 15
Progress:
- Step 1: Pick the correct typecode for the data's type and range
- Step 2: Construct the array with an iterable or bytes
- Step 3: Use list-like operations (append, extend, slicing, indexing)
- Step 4: Convert to/from bytes, lists, or files as needed
- Step 5: Validate itemsize and overflow behavior on the target platform
Step 1: Typecodes
| Code | Type | Min bytes |
|---|---|---|
'b' / 'B' | signed/unsigned char | 1 |
'u' | wchar_t (deprecated since 3.3, removed in later versions — prefer str) | 2/4 |
'h' / 'H' | signed/unsigned short | 2 |
'i' / 'I' | signed/unsigned int | 2 |
'l' / 'L' | signed/unsigned long | 4 |
'q' / 'Q' | signed/unsigned long long | 8 |
'f' | float | 4 |
'd' | double | 8 |
Use array.itemsize to check actual byte size on the running platform — it is not guaranteed identical across platforms.
Step 2: Construction
Pythonarray(typecode) # empty array array(typecode, iterable) # from list/tuple/generator of numbers array(typecode, bytes_obj) # from raw bytes (interpreted per typecode)
Step 3: Common operations
Pythona.append(6) a.extend([7, 8]) a.insert(0, -1) a.pop() a.remove(3) a.reverse() a[1:3] = array('i', [10, 20]) # slice assignment must use same typecode len(a) a.count(10) a.index(10)
Step 4: Conversion
Pythona.tobytes() # -> bytes array('i').frombytes(b) # populate from bytes a.tolist() # -> list a.tofile(f) # write to a binary file object a.fromfile(f, n) # read n items from a binary file object
Step 5: Buffer / memoryview interop
Pythonmv = memoryview(a) mv[0] = 99 # mutates the array in place
Recommendation▾
Include a comparison example contrasting array vs list vs numpy with actual memory/performance numbers to strengthen the decision guidance
Examples14 / 20
Example 1: Efficient storage of sensor readings Input:
Pythonfrom array import array readings = array('f', [23.5, 24.1, 22.9, 25.0])
Output: A compact float array using 4 bytes per element (16 bytes total) instead of Python list's per-object overhead (~28+ bytes per float object plus pointer).
Example 2: Reading binary data from a file Input:
Pythonfrom array import array a = array('i') with open('data.bin', 'rb') as f: a.frombytes(f.read())
Output: a contains the file's contents parsed as a sequence of signed ints, respecting native byte order.
Example 3: Round-tripping through bytes Input:
Pythona = array('h', [1, -2, 3]) b = a.tobytes() c = array('h') c.frombytes(b)
Output: c == a → True
Recommendation▾
The workflow checklist is somewhat generic (steps mirror the section headers) — could tie steps more directly to decision points, e.g., 'if interfacing with C, check itemsize first'
Best Practices
- Always specify typecode explicitly; don't rely on inference.
- Use
array.itemsizeandarray.buffer_info()when interfacing with C or low-level I/O to confirm memory layout. - Prefer
bytearrayormemoryviewif working with raw bytes rather than typed numeric sequences. - For large numeric datasets needing math operations (not just storage), prefer
numpy.ndarray—arraymodule has no vectorized math. - Use slice assignment with an
arrayof matching typecode, not a plain list, to avoidTypeError.
Common Pitfalls
- Mixing typecodes in slice assignment:
a[0:2] = [1, 2]raisesTypeError; must assign anotherarrayobject. - Assuming fixed itemsize across platforms:
'l'and'i'sizes can vary; always checkitemsizeif byte-exact compatibility matters. - Using
'u'typecode: deprecated/removed in modern Python — usestror dedicated Unicode handling instead. - Overflow on append: appending a value outside the typecode's range raises
OverflowError, not silent truncation. - Expecting math methods:
arrayhas nosum(),mean(), elementwise ops built in — usesum(a)(Python builtin) or convert to NumPy for numeric operations.