π£ What Inspired This Series?
The inspiration came from the real questions I encountered while preparing for Python interviews! π‘ I decided to use the Feynman Learning Techniqueβ “learning by teaching”βto solidify my understanding through explanation. While organizing and sharing, I hope to:
πΉ Help myselfβββ Clear knowledge gaps β Strengthen theoretical foundations β Understand underlying logic β Break through practical challengesπΉ Assist youβββ Make our learning and job-seeking journey smoother π
π To all Python enthusiasts: I look forward to fighting alongside my peers to improve together β’ Level up together! π€
Lists and arrays are both mutable objects and are frequently used containers in our development. While lists are flexible and simple, when faced with various requirements and the need for performance optimization with large data volumes, arrays may be the better choice. Let’s take a look at the differences in their underlying data structures.
Differences in Storage Mechanism
- Array: Stores the binary values of the original objects without needing to encapsulate them as objects;
- List: Stores references to heap objects rather than actual data values. Each element is an object that contains not only the value but also reference counts, data types, and other metadata. This results in dual overhead from object pointers and metadata;
For example, to store one million integers
- Memory usage of an array (
<span>'i'</span>type code) = Array header information + Element data = 56 + 4Γ10βΆ β 4 MB - Memory usage of a list = List object overhead + Pointer array + Element object overhead β 64 + 8Γ10βΆ + 28Γ10βΆ β 36 MB (64-bit system)
Differences in Memory Layout
- Array: Since it does not store object references, there is no need for pointer jumps, so it is arranged contiguously in memory, allowing direct data access through
<span>base address + index Γ element size</span>, reducing memory fragmentation; - List: All elements are scattered in heap memory, requiring addressing during access, leading to low CPU cache utilization;
Differences in Memory Allocation Strategy
- Array: Requires size to be provided at creation, does not need to reserve space for expansion, and allocates contiguous memory precisely, achieving nearly 100% memory utilization;
- List: Does not require size to be provided at creation, uses a dynamic expansion strategy. Although it initially allocates a small space, each expansion requires copying old data to a new memory area and reserving extra space (e.g., expanding to 1.5~2 times) to reduce frequent expansions. This reserved space leads to memory usage being higher than the actual element requirements;
Conclusion
If we need a container that only contains one type,<span>array.array</span> significantly optimizes memory efficiency and has outstanding advantages in large-scale data processing. Of course, if mixed-type data is needed, using a list will be more flexible.