Complexity Cheat Sheet for Python Operations
Last Updated :
13 Dec, 2024
Python built-in data structures like lists, sets, and dictionaries provide a large number of operations making it easier to write concise code However, not understanding the complexity of these operations can sometimes cause your programs to run slower than expected.
This cheat sheet is designed to help developers understand the average and worst-case complexities of common operations for these data structures that help them write optimized and efficient code in Python.
List Time Complexity
Python's list is an ordered, mutable sequence, often implemented as a dynamic array. Below are the time complexities for common list operations:
Operation | Examples | Average case | Amortised Worst case |
---|
Append | l.append(item) | O(1) | O(1)(resize) |
Clear | l.clear() | O(1) | O(1) |
Containment | item in/not in l | O(N) | O(N) |
Copy | l.copy() | O(N) | O(N) |
Delete | del l[i] | O(N) | O(N) |
Extend | l.extend(...) | O(N) | O(N) |
Equality | l1==l2, l1!=l2 | O(N) | O(N) |
Index | l[i] | O(1) | O(1) |
Iteration | for item in l: | O(N) | O(N) |
Length | len(l) | O(1) | O(1) |
Multiply | k*l | O(k*N) | O(k*N) |
Min, Max | min(l), max(l) | O(N) | O(N) |
Pop from end | l.pop(-1) | O(1) | O(1) |
Pop intermediate | l.pop(item) | O(N) | O(N) |
Remove | l.remove(...) | O(N) | O(N) |
Reverse | l.reverse() | O(N) | O(N) |
Slice | l[x:y] | O(y-x) | O(y-x) |
Sort | l.sort() | O(N log N) | O(N log N) |
Store by index | l[i]=item | O(1) | O(1) |
Note: Tuples have the same operations (non-mutable) and complexities.
Dictionary Time Complexity
Dictionaries in Python are implemented as hash tables, making them highly efficient for key-based operations. Here are the complexities:
Operation | Examples | Average case | Amortised Worst case |
---|
Clear | d.clear() | O(1) | O(1) |
Construction | dict(...) | O(len(d)) | O(len(d)) |
Delete | del d[k] | O(1) | O(N) |
Get | d.get() | O(1) | O(N) |
Iteration(key, value, item) | for item in d: | O(N) | O(N) |
Length | len(d) | O(1) | O(1) |
Pop | d.pop(item) | O(1) | O(N) |
Pop Item | d.popitem() | O(1) | O(1) |
Returning Views | d.values() | O(1) | O(1) |
Returning keys | d.keys() | O(1) | O(1) |
Fromkeys | d.fromkeys(seq) | O(len(seq)) | O(len(seq)) |
Note: Defaultdict has operations same as dict with same time complexity as it inherits from dict.
Set Time Complexity
Python’s set is another hash-based collection, optimized for membership checks and set operations:
Operation | Examples | Average case | Amortised Worst case |
---|
Add | s.add(item) | O(1) | O(N) |
Clear | s.clear() | O(1) | O(1) |
Copy | s.copy() | O(N) | O(N) |
Containment | item in/not in s | O(1) | O(N) |
Creation | set(...) | O(len(s)) | O(len(s)) |
Discard | s.discard(item) | O(1) | O(N) |
Difference | s1-s2 | O(len(s1)) | O(len(s1)) |
Difference Update | s1.difference_update(s2) | O(len(s2)) | - |
Equality | s1==s2, s1!=s2 | O(min(len(s1), len(s2))) | O(min(len(s1), len(s2))) |
Intersection | s1 & s2 | O(min(len(s1), len(s2))) | O(min(len(s1), len(s2))) |
Iteration | for item in s: | O(N) | O(N) |
Is Subset | s1<=s2 | O(len(s1)) | O(len(s1)) |
Is Superset | s1>=s2 | O(len(s2)) | O(len(s1)) |
Pop | s.pop() | O(1) | O(N) |
Union | s1|s2 | O(len(s1)+len(s2)) | - |
Symmetric Difference | s1^s2 | len(s1) | O(len(s1)*len(s2)) |
Tuples Time Complexity
Tuples are immutable sequences, making them lighter but with limited operations compared to lists:
Operation | Example | Average Case | Worst Case |
---|
Access by index | (e.g., t[i] ) | O(1) | O(1) |
Search | (e.g., x in t ) | O(n) | O(n) |
Iterate | (e.g., for x in t ) | O(n) | O(n) |
Strings Time Complexity
Strings are immutable and behave similarly to tuples in terms of time complexities:
Operation | Examples | Average Case | Worst Case |
---|
Access by index | (e.g., s[i] ) | O(1) | O(1) |
Concatenation | (e.g., s1 + s2 ) | O(n) | O(n) |
Search | (e.g., x in s ) | O(n) | O(n) |
Length | (e.g., len(s) ) | O(1) | O(1) |
Slice | (e.g., s[start:end] ) | O(k) | O(k) |
Iterate | (e.g., for c in s ) | O(n) | O(n) |
Key Notes -
Lists:
- Amortized complexity applies to
append()
because resizing happens occasionally. - Insertions and deletions at arbitrary positions require shifting elements.
Dictionaries and Sets:
- Hash collisions in dictionaries and sets can degrade O(1)operations to O(n).
- Worst-case complexity arises when all elements are in a single hash bucket.
Tuples and Strings:
- Both are immutable, making operations like insertion or deletion invalid.
- Access and iteration are linear due to sequential traversal requirements.
Similar Reads
Space Complexity of List Operations in Python In Python, lists are one of the most commonly used data structures. Lists are versatile, flexible, and can store items of different data types, making them suitable for a wide range of applications. However, just like any other data structure, the space complexity of list operations plays a crucial
4 min read
Python | Arithmetic operations in excel file using openpyxl Prerequisite: Reading & Writing to excel sheet using openpyxlOpenpyxl is a Python library using which one can perform multiple operations on excel files like reading, writing, arithmetic operations and plotting graphs. Let's see how to perform different arithmetic operations using openpyxl.  =S
3 min read
Operator Functions in Python | Set 1 Python has predefined functions for many mathematical, logical, relational, bitwise etc operations under the module "operator". Some of the basic functions are covered in this article. 1. add(a, b) :- This function returns addition of the given arguments. Operation - a + b. 2. sub(a, b) :- This func
5 min read
Complex Numbers in Python | Set 2 (Important Functions and Constants) Introduction to python complex numbers: Complex Numbers in Python | Set 1 (Introduction) Some more important functions and constants are discussed in this article. Operations on complex numbers : 1. exp() :- This function returns the exponent of the complex number mentioned in its argument. 2. log(x
4 min read
Why is python best suited for Competitive Coding? When it comes to Product Based Companies, they need good coders and one needs to clear the Competitive Coding round in order to reach the interview rounds. Competitive coding is one such platform that will test your mental ability and speed at the same time. Who should read this? Any programmer who
7 min read
How to Automate an Excel Sheet in Python? Before you read this article and learn automation in Python, let's watch a video of Christian Genco (a talented programmer and an entrepreneur) explaining the importance of coding by taking the example of automation.You might have laughed loudly after watching this video, and you surely might have u
8 min read