The following table summarizes common and representative list object operations.
Operation | Interpretation |
---|---|
L = [] | An empty list |
L = [123, 'abc', 1.23, {}] | Four items: indexes 0..3 |
L = ['Bob', 40.0, ['dev', 'mgr']] | Nested sublists |
L = list('test') L = list(range(-4, 4)) | List of an iterable's items, list of successive integers |
L[i] L[i][j] L[i:j] len(L) | Index, index of index, slice, length |
L1 + L2 L * 3 | Concatenate, repeat |
for x in L: print(x) 3 in L | Iteration, membership |
L.append(4) L.extend([5,6,7]) L.insert(i, X) | Methods: growing |
L.index(X) L.count(X) | Methods: searching |
L.sort() | Methods: sorting, reversing, |
L.reverse() L.copy() L.clear() | copying (3.3+), clearing (3.3+) |
L.pop(i) L.remove(X) del L[i] del L[i:j] L[i:j] = [] | Methods, statements: shrinking |
L[i] = 3 L[i:j] = [4,5,6] | Index assignment, slice assignment |
L = [x**2 for x in range(5)] list(map(ord, 'test')) | List comprehensions and maps (Chapter 4, Chapter 14, Chapter 20) |