Learning outcomes
By the end of this lecture, you should be able to:- define a list and create one correctly
- access elements using index
- update list values
- explain why lists are useful for grouped data
What is a list?
- A list is an ordered, mutable collection.
- Ordered means elements have positions.
- Mutable means elements can be changed after creation.
Why lists matter
Without lists, storing many similar values becomes messy:Indexing
- first index is
0 - negative index counts from end
Updating list values
Length and membership
Slicing lists
Exam hints and traps
list[index]gives one elementlist[start:end]gives a new list slice- list is mutable, unlike string and tuple
- invalid index raises
IndexError
Quick practice
- Create a list of 5 colors.
- Print first and last element of a list.
- Replace third element of
[1, 2, 3, 4]with99.
