A list stores many values in one ordered collection, written in square brackets: marks = [85, 92, 78]. Lists are the workhorse data structure of Python — and unlike strings, they're mutable, meaning you can change them after creation. That one difference unlocks a lot.
1Creating, indexing and changing lists
A list holds an ordered sequence of items, which can even be of different types. You index and slice it exactly like a string (starting at 0, negative indices, [start:stop]) — but because lists are mutable, you can also assign to a position to change it:
fruits = ["apple", "mango", "kiwi"] print(fruits[0]) # apple fruits[1] = "banana" # allowed! lists are mutable print(fruits) # ['apple', 'banana', 'kiwi']
Click Run to execute.
- A list is an ordered collection in square brackets; items can be different types.
- Index and slice like strings (0-based, negative indices, [start:stop]).
- Lists are mutable: you can change an item with
list[i] = value(strings can't).
2List methods
Lists have a rich set of methods for adding, removing and rearranging items:
| Method | Does |
|---|---|
append(x) | Add x to the end |
insert(i, x) | Insert x at index i |
extend(list2) | Add all items of another list |
remove(x) | Remove the first x |
pop(i) | Remove and return the item at i (last if no i) |
sort() / reverse() | Sort / reverse the list in place |
index(x) / count(x) | Find position of x / count how many x |
Useful built-in functions also work on lists: len(), min(), max(), sum(), and sorted() (which returns a new sorted list without changing the original).
Click Run to execute.
- Add: append (end), insert (at index), extend (another list). Remove: remove (by value), pop (by index).
- sort() and reverse() change the list in place; sorted() returns a new sorted list.
len(),min(),max(),sum()also work on lists.
3Nested lists and matrices
A list can contain other lists. This nested list is how you represent a grid or a matrix (a table of rows and columns):
matrix = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]You reach an inner item with two indices: matrix[0] is the first row [1, 2, 3], and matrix[0][2] is the item in row 0, column 2 → 3. Nested loops (a loop inside a loop) are the natural way to walk through every cell.
Click Run to execute.
- A nested list (list of lists) represents a grid or matrix of rows and columns.
- Use two indices:
matrix[row][col]. - Nested loops walk through every cell of a matrix.
★ Project: a marks manager
In the playground, build a small marks program:
- Make a list of 5 test marks.
- Add a new mark with append(), then remove the lowest using remove(min(marks)).
- Print the highest, lowest and average (use max, min, sum and len).
- Sort the list and print it from highest to lowest (sort then reverse).
Ready for the chapter test?
Answer 5 questions. Score 60% or more to mark this chapter complete.
Start the test →💡 Log in to save your progress and earn the certificate.