Low-level arrays
Referential Arrays
[Data structure and algorithms in Python], P 187
To avoid wasting spaces when storing an array of strings, Python represents a list or tuple instance using an internal storage mechanism of an array of object references
, (This is somehow different from using Numpy
).
As shown above, when computing a slice of a list, the result is a new list instance, but that new list has references to the same elements that are in the original list.
Compact Arrays in Python
Strings
are represented using an array of characters
(not an array of references
), which is called a compact
array. Compact arrays have several advantages over referential structures in terms of computing performance.
Another important advantage to a compact structure for high-performance computing is that the primary data are stored consecutively in memory.
We can create a compact array as
primes = array('i', [2, 3, 5, 7, 11, 13, 17])
Compact arrays of such structures are based on C programming language and does NOT support user-defined data types.