Variables hold one value each. What if you have six values that belong together — six numbers, six names — and you want them under one name?
A list of variables? Six different ones?
Python has a built-in collection for this: a list. Square brackets, comma-separated values:
numbers = [4, 8, 15, 16, 23, 42]
print(numbers) # [4, 8, 15, 16, 23, 42]One name (numbers) holds all six values. Order is preserved.
How do I get one specific value out?
Square-bracket indexing. The first item is at index 0 (not 1 — programming convention):
print(numbers[0]) # 4 — first item
print(numbers[5]) # 42 — sixth itemAnd the last item — index 5? What if the list grows and I don't know the last index?
Use negative indices: numbers[-1] is always the last item, numbers[-2] is the second-to-last. Python counts backwards from the end when the index is negative. Saves you from numbers[len(numbers) - 1].
What if I ask for an index that doesn't exist?
IndexError. The program crashes with a clear message: list index out of range. There's no "return None silently" fallback — if you might be off the end, check with len() first.
A list holds multiple values under one name. Square brackets, comma-separated:
numbers = [4, 8, 15, 16, 23, 42]
names = ["Ada", "Bob", "Cleo"]
mixed = [1, "two", 3.0, True] # any types, any combination
empty = [] # zero itemsOrder is preserved — you get items back in the order you put them in.
Access one item with list[index]. Indices start at 0:
numbers[0] # 4 — first
numbers[1] # 8 — second
numbers[5] # 42 — sixth (and last)Negative indices count from the end:
numbers[-1] # 42 — last
numbers[-2] # 23 — second-to-lastIndexErrornumbers[10] # IndexError: list index out of rangeNo silent failure. If you might be off the end, check with len(list) first.
Reassign an item by index:
numbers[0] = 99
print(numbers) # [99, 8, 15, 16, 23, 42]Lists are mutable — you can change them after they're created. (Strings, by contrast, are immutable — you can't replace a single character of a string in-place; you build a new string.)
A list can hold lists:
grid = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(grid[1][2]) # 6 — second row, third columnNot a built-in 2D primitive — just lists of lists. Rare in beginner code; come back when you need it.
Variables hold one value each. What if you have six values that belong together — six numbers, six names — and you want them under one name?
A list of variables? Six different ones?
Python has a built-in collection for this: a list. Square brackets, comma-separated values:
numbers = [4, 8, 15, 16, 23, 42]
print(numbers) # [4, 8, 15, 16, 23, 42]One name (numbers) holds all six values. Order is preserved.
How do I get one specific value out?
Square-bracket indexing. The first item is at index 0 (not 1 — programming convention):
print(numbers[0]) # 4 — first item
print(numbers[5]) # 42 — sixth itemAnd the last item — index 5? What if the list grows and I don't know the last index?
Use negative indices: numbers[-1] is always the last item, numbers[-2] is the second-to-last. Python counts backwards from the end when the index is negative. Saves you from numbers[len(numbers) - 1].
What if I ask for an index that doesn't exist?
IndexError. The program crashes with a clear message: list index out of range. There's no "return None silently" fallback — if you might be off the end, check with len() first.
A list holds multiple values under one name. Square brackets, comma-separated:
numbers = [4, 8, 15, 16, 23, 42]
names = ["Ada", "Bob", "Cleo"]
mixed = [1, "two", 3.0, True] # any types, any combination
empty = [] # zero itemsOrder is preserved — you get items back in the order you put them in.
Access one item with list[index]. Indices start at 0:
numbers[0] # 4 — first
numbers[1] # 8 — second
numbers[5] # 42 — sixth (and last)Negative indices count from the end:
numbers[-1] # 42 — last
numbers[-2] # 23 — second-to-lastIndexErrornumbers[10] # IndexError: list index out of rangeNo silent failure. If you might be off the end, check with len(list) first.
Reassign an item by index:
numbers[0] = 99
print(numbers) # [99, 8, 15, 16, 23, 42]Lists are mutable — you can change them after they're created. (Strings, by contrast, are immutable — you can't replace a single character of a string in-place; you build a new string.)
A list can hold lists:
grid = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(grid[1][2]) # 6 — second row, third columnNot a built-in 2D primitive — just lists of lists. Rare in beginner code; come back when you need it.
Create a free account to get started. Paid plans unlock all tracks.