Skip to content
Related Articles
Open in App
Not now

Related Articles

Complexity Cheat Sheet for Python Operations

Improve Article
Save Article
  • Difficulty Level : Hard
  • Last Updated : 17 Aug, 2022
Improve Article
Save Article

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. 

Prerequisite: List, Dictionaries, Sets 

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) for Python2, O(1) for Python3 and the latter has O(1) which can create a lot of differences in nested statements. 

Important points:  

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

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

List Operations

OperationExamplesComplexity class
 Average caseAmortised Worst case
Appendl.append(item)O(1)O(1)
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))
Storel[i]=itemO(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

OperationExamplesComplexity class
 Average 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 Operations 

OperationExamplesComplexity class
 Average 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))

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

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

 

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!