A program chooses a container for changing item labels versus many same-type sensor values.
What You Need to Understand
A list is a general mutable sequence; a tuple is a fixed ordered sequence; array.array is a specialized typed array from the standard library that stores compatible numeric values compactly. NumPy arrays are third-party and have broader numerical capabilities.
When to Use It
A program chooses a container for changing item labels versus many same-type sensor values.
Step-by-Step Python Example
from array import array
labels = ["A", "B"]
point = (3, 5)
readings = array("i", [10, 20, 30])
print(labels, point)
print(readings.tolist())
How the Example Works
The list stores labels, the tuple groups coordinates, and the integer array stores values of one declared type.
Example Output
['A', 'B'] (3, 5)
[10, 20, 30]
[1, 2, 3]
A Second Example
Compare this smaller variation with the main example, then experiment with your own values.
from array import array
values = array("i", [1, 2])
values.append(3)
print(values.tolist())
Important Behavior and Edge Cases
An array.array is not a NumPy array. Type codes and supported sizes vary, and it cannot store arbitrary mixed objects.
Try It Yourself
Try appending a float to an integer array and explain why it fails.
Common Beginner Mistakes
- An
array.arrayis not a NumPy array. Type codes and supported sizes vary, and it cannot store arbitrary mixed objects. - Overlooking how an iterable is consumed, how element positions are chosen, or whether a function creates a new list.
Key Takeaways
- A list is a general mutable sequence; a tuple is a fixed ordered sequence;
array.arrayis a specialized typed array from the standard library that stores compatible numeric values compactly. - The list stores labels, the tuple groups coordinates, and the integer array stores values of one declared type.
- An
array.arrayis not a NumPy array.