Lists: Ordered and Changeable
Scenario: A queue of services changes as incidents open and close.
New words, in plain English
- List - an ordered, mutable collection
- Mutable - able to change after creation
- Element - one value in a collection
Lists use square brackets. Index and slice them like strings. append, extend, insert, remove, pop, sort, and reverse modify a list; sorted returns a new list.
Analogy: A Python program is like a clear set of instructions for a careful helper: names label things, indentation groups steps, and errors explain where the helper became confused.
A small, real example
queue = ["api", "db"]
queue.append("cache")
finished = queue.pop(0)
print(finished, queue)
Tip: Copy withitems.copy()oritems[:]when two independent lists are required.
Goal: Practice this idea in the py-list-basics challenge at/labs/python. Fix the broken program, run it yourself, then usecheckto prove the real end state.