Lists
Ordered and changeable
Ordered, changeable collections
Unit 3 · Data Structures
A data structure organizes values so a program can find and work with them. Explore four built-in Python structures and use each lab to see how its rules affect real data.
Ordered and changeable
Ordered and fixed
Unique values
Key → value lookup
Ordered · changeable · duplicates allowed
A list keeps its items in order. Each position has an index starting at 0, so the first item is at index 0 and the last item is at index -1. Lists can grow, shrink, and be edited while a program runs.
Think of a supply tray: each compartment has a position, and you can add, replace, or remove an item without rebuilding the tray.
Choose an operation, enter an item, then watch how the indexed row changes.
Choose an operation to change the list. Length: 3
append(x)Add one item at the end.
insert(i, x)Place an item at a chosen index.
remove(x)Remove the first matching value.
supplies = ["Notebook","Pencil","Ruler"]
supplies.append("Eraser")
print("Items:", supplies)
print("Count:", len(supplies))Output
Your program's output will appear here.
NameError: name 'mark' is not defined
NameError: A name was used before Python could find a variable or function with that name.
IndexError: list index out of range
IndexError: The requested position is outside the items in this list or sequence.
KeyError: 'email'
KeyError: That key does not exist in this dictionary.
TypeError: can only concatenate str (not "int") to str
TypeError: This operation does not work with the kind of value it received.
ZeroDivisionError: division by zero
ZeroDivisionError: A number cannot be divided by zero.
SyntaxError: invalid syntax
SyntaxError: Python could not understand the structure of this line. Check spelling, brackets, colons, and quotes.
IndentationError: expected an indented block
IndentationError: The spaces at the start of a line do not match Python's block structure.
remove(value); remove a position and receive its value with pop(index).1. What is the index of the first list item?
2. Which method adds one item to the end of a list?
Ask two questions: does order matter, and should the collection change after creation?
| Structure | Ordered? | Changeable? | Access values by |
|---|---|---|---|
| list | Yes | Yes | Position (index) |
| tuple | Yes | No | Position (index) |
| set | No guaranteed order | Yes | Membership, not index |
| dict | Insertion order is preserved | Yes | Key |
Use a list for a sequence that changes, a tuple for a fixed record, a set for unique membership, and a dictionary for named lookups.