PCC·03 · Chapter record
Introducing Lists
Chapter 3 — storing, changing, and organizing sets of data
Five sessions on Python's most useful data structure: creating a list and indexing into it, changing and adding elements, removing them, organizing them, and the classic IndexError mistake. Each one explains the concept, lists the key terms, then gives you exercises — try them yourself before revealing the code.
What Is a List?
Concept
- A list is a collection of items in a particular order, written with square brackets [] and items separated by commas.
- A list usually holds more than one item, so it's good style to give it a plural name —
bicycles, notbicycle. - Access one item by its index — the item's position inside square brackets, like
bicycles[0]. - Python starts counting at 0, not 1 — the first item is index 0, the second is index 1, and so on.
- Index
-1always returns the last item, no matter how long the list is —-2is the second-to-last, and so on.
Key Terms
- list
- an ordered collection of items in square brackets
- element
- a single item stored in a list
- index
- an item's position in a list, starting at 0
Exercise
Store a few friends' names in a list, then print each one by indexing it individually (0, 1, 2…).
Using that same list, print a short personalized message to each friend by indexing into the list.
Make a list of your favorite mode of transportation (e.g. car or bike brands), then print a sentence using one indexed item.
Modifying & Adding Elements
Concept
- Change an item the same way you access it — give the index, then assign a new value:
motorcycles[0] = 'ducati'. .append()adds a new item to the end of a list — the simplest way to build one dynamically, often starting from an empty list[]..insert()adds an item at any position by index — it shifts every item after that position one spot to the right.
Key Terms
- .append(value)
- adds value to the end of the list
- .insert(i, value)
- adds value at index i, shifting later items right
Exercise
Make a guest list of at least three people you'd love to have dinner with, then print an invitation line for each.
One guest cancels — print a note about who dropped out, replace their name in the list with someone new, then print fresh invitations.
You found more space — use insert() to add one guest at the start, insert() for one in the middle, and append() for one at the end.
Removing Elements
Concept
delremoves an item by index and discards it completely — use it when you no longer need the value..pop()also removes an item — by default the last one — but hands the value back so you can still use it..pop(i)removes from any position..remove(value)deletes an item by its value instead of its position — handy when you know what to remove but not exactly where it is.
Key Terms
- del list[i]
- removes the item at index i and discards it
- .pop(i)
- removes the item at index i (default: last) and returns it
- .remove(value)
- removes the first item matching value
Exercise
Start from a 5-person guest list. Use pop() to remove people one at a time until only two remain, printing a goodbye message each time.
Use del to clear the final two names, then print the list to confirm it's empty.
Organizing a List
Concept
.sort()rearranges a list alphabetically (or numerically) and keeps that order permanently; passreverse=Trueto sort the other way.sorted()shows a list in order without changing the original — useful for a sorted display while keeping the real order intact..reverse()flips a list's current order in place; run it again and it goes right back.len()tells you how many items are in a list, from counting aliens left on screen to counting registered users.
Key Terms
- .sort()
- sorts a list in place, permanently
- sorted()
- returns a sorted copy without changing the original
- .reverse()
- flips a list's order in place
- len()
- returns the number of items in a list
Exercise
List five places you'd like to visit, not in alphabetical order. Show them sorted with sorted() without changing the original, then confirm the original order is still intact.
Reverse your list with reverse(), then reverse it again to restore the original order.
Print how many places are in your list using len().
Avoiding Index Errors
Concept
- Ask for an index that doesn't exist — like item 3 in a 3-item list — and Python raises an IndexError, since valid indexes only go up to
len(list) - 1. - This is the classic off-by-one mistake: people count the third item as index 3, but Python's third item is index 2.
- Index
-1is always safe for a non-empty list, but even that fails on an empty list — there's nothing to return.
Key Terms
- IndexError
- raised when you ask for an index that doesn't exist in the list
- off-by-one error
- a mistake from miscounting the first/last valid index by one
Exercise
For a 4-item list, what's the largest valid index? Try accessing it, then try one past it and read the error that comes back.