Open In App

Complexity Cheat Sheet for Python Operations

Last Updated : 13 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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.


Next Article
Article Tags :
Practice Tags :

Similar Reads