Complexity Cheat Sheet for Python Operations

Prerequisite: List, Dictionaries, Sets

Python built-in data structures like list, sets, dictionaries provide a large number of operations making it easier to write concise code but not being aware of their complexity can result in unexpected slow behavior of your python code.

For example:

A simple dictionary lookup Operation can be done by either :

if key in d:

or



if dict.get(key)

The first has a time complexity of O(N) and the latter has O(1) which can create a lot of difference in nested statements.

Important points:

  1. Lists are similiar to arrays with bidirectional adding and deleting capability.
  2. Dictionaries and Set use Hash Tables for insertion/deletion and lookup operations.

This Cheatsheet can be referred for choosing operations that are efficient with respect to time.

List Operations

Operation Examples Complexity class
Average case Amortised Worst case
Append l.append(item) O(1) O(1)
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 l[i]=item O(1) O(1)

For more information, refer to Internal working of list in Python.

Note: Tuples have the same operations (non-mutable) and complexities.

Dictionary Operations

Operation Examples Complexity class
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.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 Operations

Operation Examples Complexity class
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))

For more information, refer to Internal working of Set in Python

Note: Frozen sets have the same operations (non-mutable) and complexities.

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

4


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.