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:

OperationExamplesAverage caseAmortised Worst case
Appendl.append(item)O(1)O(1)(resize)
Clearl.clear()O(1)O(1)
Containmentitem in/not in lO(N)O(N)
Copyl.copy()O(N)O(N)
Deletedel l[i]O(N)O(N)
Extendl.extend(...)O(N)O(N)
Equalityl1==l2, l1!=l2O(N)O(N)
Indexl[i]O(1)O(1)
Iterationfor item in l:O(N)O(N)
Lengthlen(l)O(1)O(1)
Multiplyk*lO(k*N)O(k*N)
Min, Maxmin(l), max(l)O(N)O(N)
Pop from endl.pop(-1)O(1)O(1)
Pop intermediatel.pop(item)O(N)O(N)
Removel.remove(...)O(N)O(N)
Reversel.reverse()O(N)O(N)
Slicel[x:y]O(y-x)O(y-x)
Sortl.sort()O(N log N)O(N log N)
Store by indexl[i]=itemO(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:

OperationExamplesAverage caseAmortised Worst case
Cleard.clear()O(1)O(1)
Constructiondict(...)O(len(d))O(len(d))
Deletedel d[k]O(1)O(N)
Getd.get()O(1)O(N)
Iteration(key, value, item)for item in d:O(N)O(N)
Lengthlen(d)O(1)O(1)
Popd.pop(item)O(1)O(N)
Pop Itemd.popitem()O(1)O(1)
Returning Viewsd.values()O(1)O(1)
Returning keysd.keys()O(1)O(1)
Fromkeysd.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:

OperationExamplesAverage caseAmortised Worst case
Adds.add(item)O(1)O(N)
Clears.clear()O(1)O(1)
Copys.copy()O(N)O(N)
Containmentitem in/not in sO(1)O(N)
Creationset(...)O(len(s))O(len(s))
Discards.discard(item)O(1)O(N)
Differences1-s2O(len(s1))O(len(s1))
Difference Updates1.difference_update(s2)O(len(s2))-
Equalitys1==s2, s1!=s2O(min(len(s1), len(s2)))O(min(len(s1), len(s2)))
Intersections1 & s2O(min(len(s1), len(s2)))O(min(len(s1), len(s2)))
Iterationfor item in s:O(N)O(N)
Is Subsets1<=s2O(len(s1))O(len(s1))
Is Supersets1>=s2O(len(s2))O(len(s1))
Pops.pop()O(1)O(N)
Unions1|s2O(len(s1)+len(s2))-
Symmetric Differences1^s2len(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 CaseWorst 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 CaseWorst 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