Python list

The List ADT is internally represented by an Array

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.

OperationExamplesAverage CaseAmortised Worst Case
Appendl.append(item)
Clearl.clear()
Containmentitem in/not in l
Copyl.copy()
Deletedel l[i]
Extendl.extend(…)
Equalityl1==l2, l1!=l2
Indexl[i]
Iterationfor item in l:
Lengthlen(l)
Multiplyk*l
Min, Maxmin(l), max(l)
Pop from endl.pop(-1)
Pop intermediatel.pop(item)
Removel.remove(…)
Reversel.reverse()
Slicel[x:y]
Sortl.sort()
Storel[i]=item

Python dict

Python implements the Dictionary ADT using a Hash table

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

OperationExamplesAverage CaseAmortised Worst Case
Adds.add(item)
Clears.clear()
Copys.copy()
Containmentitem in/not in s
Creationset(…)
Discards.discard(item)
Differences1-s2
Difference Updates1.difference_update(s2)
Equalitys1==s2, s1!=s2
Intersections1 & s2
Iterationfor item in s:
Is Subsets1<=s2
Is Supersets1>=s2
Pops.pop()
Unions1|s2
Symmetric Differences1^s2

Sources: 1, 2

  • task Transcribe screenshots