10.2 Python Common Data Structures
Similar to C++ STL, Python also provides various data structures (the underlying details may vary depending on the implementation):
-
Sequence Containers: Containers that maintain order.
- list: A
dynamic array
, one of the most commonly used data structures for random access. Since most algorithm complexities exceed , lists are frequently used to store data or intermediate results. As adding or removing elements at the end has complexity, lists can also be used as stacks. - tuple: An
immutable list
, where neither the elements nor the length can be modified. - collections.deque: A
double-ended queue
, a powerful data structure supporting random access as well as insertion and deletion at both ends (making it useful as a stack or queue). It has some overhead but can approximate a doubly linked list.
- list: A
-
Container Adaptors: Containers built on top of other data structures.
- heapq: A
min-heap
(a data structure where the smallest element is accessed first), implemented using lists. It supports array sorting in , value insertion in , access to the smallest value in , and deletion of the smallest value in .heapq
is often used for maintaining data structures and quickly accessing the smallest value, but it doesn't support custom comparison functions. Typically, we precompute the custom values and store them as tuples, like(custom_value, index)
, in the heap. This way, tuple comparisons are done from left to right, comparing the custom value first and then the insertion order.
- heapq: A
-
Ordered Associative Containers:
- collections.OrderedDict:
Ordered mapping or ordered table
. Note that "Ordered" here means maintaining the insertion order, unlike C++'s map, which sorts keys by size.OrderedDict
is particularly useful for implementing LRU (Least Recently Used) caching.
- collections.OrderedDict:
-
Unordered Associative Containers:
- set:
Hash set
. Allows for fast insertion, lookup, and deletion of elements in O(1) time. Commonly used to quickly check if an element exists in the container. - dict:
Hash map or hash table
. Builds upon the set structure by adding key-value mapping. In some scenarios, if the key range is known and small, a list can replace a dict, using indices to represent keys and list values for their corresponding values. - collections.Counter:
Counter
. A specialized version of dict that accepts a list and counts the frequency of each element. Keys are the element values, and values are their frequencies. This can serve as a multiset.
- set:
Similarly, since this is not a book on Python internals, readers are encouraged to explore more details about these data structures. Understanding their principles and usage will enable more effective problem-solving in algorithms and data structures.