Python list
The costly operations are inserting and deleting items near the beginning (as everything has to be moved). Insert at the end also becomes costly if preallocated space becomes full.
Operation | Examples | Average Case | Amortised Worst Case |
---|---|---|---|
Append | l.append(item) | ||
Clear | l.clear() | ||
Containment | item in/not in l | ||
Copy | l.copy() | ||
Delete | del l[i] | ||
Extend | l.extend(…) | ||
Equality | l1==l2, l1!=l2 | ||
Index | l[i] | ||
Iteration | for item in l: | ||
Length | len(l) | ||
Multiply | k*l | ||
Min, Max | min(l), max(l) | ||
Pop from end | l.pop(-1) | ||
Pop intermediate | l.pop(item) | ||
Remove | l.remove(…) | ||
Reverse | l.reverse() | ||
Slice | l[x:y] | ||
Sort | l.sort() | ||
Store | l[i]=item |
Python dict
Python implements the Dictionary ADT using a Hash table
Operation | Examples | Average Case | Amortised Worst Case |
---|---|---|---|
Clear | d.clear() | ||
Construction | dict(...) | ||
Delete | del d[k] | ||
Get | d.get() | ||
Iteration(key, value, item) | for item in d: | ||
Length | len(d) | ||
Pop | d.pop(item) | ||
Pop Item | d.popitem() | ||
Returning Views | d.values() | ||
Returning keys | d.keys() | ||
Fromkeys | d.fromkeys(seq) |
Python set
Python implements the Set ADT using a Hash table
The major advantage of using a Set, as opposed to a List, is that it has a highly optimised method for checking whether a specific element is contained in the set (using the hash table)
Operation | Examples | Average Case | Amortised Worst Case |
---|---|---|---|
Add | s.add(item) | ||
Clear | s.clear() | ||
Copy | s.copy() | ||
Containment | item in/not in s | ||
Creation | set(…) | ||
Discard | s.discard(item) | ||
Difference | s1-s2 | ||
Difference Update | s1.difference_update(s2) | ||
Equality | s1==s2, s1!=s2 | ||
Intersection | s1 & s2 | ||
Iteration | for item in s: | ||
Is Subset | s1<=s2 | ||
Is Superset | s1>=s2 | ||
Pop | s.pop() | ||
Union | s1|s2 | ||
Symmetric Difference | s1^s2 |
- task Transcribe screenshots