Say you are dealing with 8 byte floating point numbers. "Packed bytes" in this context means that there is a allocated fragment of allocated memory in which the first 8 bytes represent the first float, and then immediately the next 8 bytes represent the next float, etc. No loss. This is the most economical way to store data (at least without compression). It may also be the most time-efficient for certain operations (e.g. massive arithmetic operations).
Python list
does not save things this way. First, one list item may be a float, but the next may be a different type of object. Alternatively, you can remove, insert, or replace items in a list. Some of these operations are related to lengthening or shortening the list dynamically. All of them are very inefficient in time and memory if the elements are stored as packed bytes. The Python list
class is designed to be as versatile as possible, making trade-offs between the effectiveness of various types of operations.
Probably the most important difference is that the Python list
in its basic C implementation is a container full of pointers to objects, not a container full of contents of the original object. One consequence of this is that multiple references to the same Python object may appear in list
. Another is that changing a specific item can be performed very efficiently. For example, suppose the first item in your list, a[0]
, is an integer, but you want to replace it with a string that takes up more memory, for example. a[0] = "There a horse in aisle five."
A packed array should (a) make an extra room by shifting the rest of the contents of the array in memory and (b) separately update some index of sizes and types of elements. But a Python list
would only have to overwrite one pointer value with another.
In a CPython implementation, the pointers themselves can (more or less) be packed into memory. This means that adding a new item to the list
will still be ineffective (as to how it would be implemented in the implementation of the Python list
, say, the structure of the list of links under the hood).
In general, there is no absolute “effective” or “ineffective” - all this is a question of what resource you are working with, what (limitations) content types are in the container and how you intend to transform the container or its contents.
source share