Python Data Structures & Time Complexity
Reference
Technical Reference Guide • Computer Science & Software Engineering
1. Overview of Core Built-in Types
• List: Dynamic array supporting fast random access ($O(1)$) and variable-length sequencing.
• Tuple: Immutable ordered sequence used for fixed data records and dictionary keys.
• Set: Unordered collection of unique elements implemented via a hash table.
• Dictionary (dict): Key-value hash map providing average $O(1)$ lookups, insertions, and deletions.
2. Asymptotic Time Complexities
Structure Access Search Insertion Deletion
List O(1) O(n) O(n) [O(1) at end] O(n)
Tuple O(1) O(n) N/A (Immutable) N/A (Immutable)
Set N/A O(1) avg O(1) avg O(1) avg
Dict O(1) avg O(1) avg O(1) avg O(1) avg
3. Performance Optimization Rules
• Use [Link] when implementing FIFO queues or double-ended stacks to avoid $O(n)$ element shifting
penalties on [Link](0).
• Use generator expressions instead of list comprehensions when processing large streams of sequential data to
keep memory usage minimal.
• Prefer in operators against set or dict collections rather than lists when filtering high-volume datasets.