Cheat Sheets
Lists · Study material
Lists Cheat Sheet
| Code | Result / meaning |
|---|---|
a = [3, 1, 2]; a[0], a[-1], a[1:] | 3, 2, [1, 2] |
a.append(4) / a.extend([5, 6]) | add one / many |
a.insert(0, 9) | insert at the front |
a.remove(9), a.pop(), a.pop(0), del a[1] | remove by value / position |
a.index(2), a.count(2), 2 in a | search |
a.sort(), sorted(a, reverse=True), key=len | sorting |
a.reverse(), a[::-1] | reverse in place / copy |
min(a), max(a), sum(a), len(a) | aggregates |
first, *rest = a | unpacking |
a[1:3] = [7, 8, 9] | slice assignment |
[[0] * c for _ in range(r)] | r × c grid |
a.copy(), copy.deepcopy(a) | shallow / deep copy |