~/learn/Python · Collections
Lists and loops
Ordered collections and the for loop that walks them.
why this matters
Lists hold collections — a list of prices, a list of names. Almost all data work loops over one.
A list is an ordered, mutable sequence. You append to it, index into it and slice it.
A for loop iterates over any sequence. enumerate gives you the index alongside the value, and zip walks two sequences in step.
A list comprehension builds a new list from an old one in a single expression, with an optional filter — it replaces most short accumulation loops.
Counting from zero
Indexes start at 0. The first item is items[0] and the last is items[-1].
key takeaways
- Lists are ordered and changeable.
- items[1:3] slices positions 1 and 2.
- .append() adds to the end.
check your understanding
nums = [10, 20, 30, 40]. What is nums[1:3]?
free practice
Build confidence with a question you can solve without Pro.