The array Module
The array
module supplies a type, also called array
, whose instances are mutable sequences, like lists. An array
a
is a one-dimensional sequence whose items can be only characters, or only numbers of one specific numeric type, fixed when you create a
.
array.array
’s main advantage is that, compared to a list, it can save memory to hold objects all of the same (numeric or character) type. An array
object a
has a one-character, read-only attribute a
.typecode
, which is set on creation and gives the type of a
’s items. Table 16-1 shows the possible typecodes for array
.
Table 16-1. Typecodes for the array module
Typecode | C type | Python type | Minimum size |
---|---|---|---|
|
|
| 1 byte |
|
|
| 1 byte |
|
|
| 1 byte |
|
|
| 2 bytes |
|
|
| 2 bytes |
|
|
| 2 bytes |
|
|
| 2 bytes |
|
|
| 2 bytes |
|
|
| 4 bytes |
|
|
| 4 bytes |
|
|
| 4 bytes |
|
|
| 8 bytes |
The size in bytes of each item may be larger than the minimum, depending on the machine’s architecture, and is available as the read-only attribute a
.itemsize
. Module array
supplies just the type object called array
.
array |
Creates and returns an |
Get Python in a Nutshell, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.