Lists and Tuples
Lists — ordered, mutable sequences
Section titled “Lists — ordered, mutable sequences”A list is Python’s workhorse container. It holds an ordered sequence of any objects and supports in-place mutation.
fruits = ['apple', 'banana', 'cherry']fruits.append('date') # add to endfruits.insert(1, 'avocado') # insert before index 1fruits.remove('banana') # remove first occurrenceprint(fruits) # ['apple', 'avocado', 'cherry', 'date']Common list methods
Section titled “Common list methods”| Method | Description |
|---|---|
append(x) | Add x to the end |
insert(i, x) | Insert x before index i |
remove(x) | Remove first occurrence of x |
pop(i=-1) | Remove and return item at index i |
sort(key=None, reverse=False) | Sort in place |
reverse() | Reverse in place |
index(x) | Return index of first occurrence of x |
count(x) | Count occurrences of x |
Sorting
Section titled “Sorting”list.sort() sorts in place and returns None.
Pass a key function to sort by an arbitrary criterion.
numbers = [5, 2, 8, 1, 9, 3]numbers.sort()print(numbers) # [1, 2, 3, 5, 8, 9]
words = ['banana', 'apple', 'cherry']words.sort(key=len)print(words) # ['apple', 'banana', 'cherry']Use sorted(iterable) instead of .sort() when you need a new list and want to leave the original unchanged.
Tuples — ordered, immutable sequences
Section titled “Tuples — ordered, immutable sequences”A tuple is like a list but cannot be modified after creation. Use tuples to represent fixed records where position carries meaning.
rgb = (255, 128, 0) # an orange colourpoint = (3.0, 4.0) # a 2-D coordinaterow = (1, 'Alice', 42) # a database rowTuple unpacking
Section titled “Tuple unpacking”Python lets you unpack a tuple (or any iterable) directly into variables.
The number of variables must match the length — unless you use * to capture the rest.
coords = (10.5, 20.3, 30.1)x, y, z = coordsprint(x, y, z) # 10.5 20.3 30.1
first, *rest = [1, 2, 3, 4, 5]print(first) # 1print(rest) # [2, 3, 4, 5]Accessing tuple fields by index
Section titled “Accessing tuple fields by index”Without a name, tuple fields are accessed by integer index. Assign constants for readability:
RGB = (255, 128, 0)RED = 0GREEN = 1BLUE = 2
print(RGB[RED]) # 255print(RGB[GREEN]) # 128print(RGB[BLUE]) # 0For a more ergonomic approach, use collections.namedtuple or typing.NamedTuple.
Full runnable demo
Section titled “Full runnable demo”# --- list mutation and sorting ---fruits = ['apple', 'banana', 'cherry']fruits.append('date')fruits.insert(1, 'avocado')print('after insert:', fruits)
fruits.remove('banana')print('after remove:', fruits)
numbers = [5, 2, 8, 1, 9, 3]numbers.sort()print('sorted:', numbers)
words = ['banana', 'apple', 'cherry']words.sort(key=len)print('by length:', words)
# --- tuple unpacking ---coords = (10.5, 20.3, 30.1)x, y, z = coordsprint('unpacked:', x, y, z)
first, *rest = [1, 2, 3, 4, 5]print('first:', first)print('rest:', rest)
# --- named fields via index constants ---RGB = (255, 128, 0)print('red:', RGB[0], 'green:', RGB[1], 'blue:', RGB[2])Loading Python runtime (first run only)…