cities[0]Zero-based positions count from the start.
Learn with diagrams, code, systems and practical examples.
Store an ordered, changeable collection of values.
Lists are a natural choice when order matters and you may add, remove, or update items.
A list holds values in order. Positions are zero-based indexes, and common operations include append, remove, slicing, and iteration.
Run this example first. Do not change several things at once; confirm the basic behavior, then experiment.
tasks = ["email", "deploy"]
tasks.append("review")
print(tasks[0])
print(tasks) A list holds values in order. Positions are zero-based indexes, and common operations include append, remove, slicing, and iteration.
email
['email', 'deploy', 'review']The example is intentionally small. Focus on the chapter’s main idea before adding extra syntax or framework code.
Using an index without checking whether the list has that position.
Prefer iteration when you need every item, and validate indexes when positions come from external input.
These are deliberately small. If you can complete them without copying the example, you are ready to continue.
A Python list is an ordered, mutable container. In plain language: it keeps items in a sequence, lets you reach items by position, and lets you change the sequence later. Before memorizing methods, ask two questions: which list object am I changing? and does this operation mutate that list or create a new value?
cities = ["Surrey", "Vancouver", "Burnaby"]
print(cities[0])
print(cities[-1])
print(cities[0:2])
print("Vancouver" in cities)
cities[1] = "Richmond"
print(cities)
Expected output: Surrey; Burnaby; ['Surrey', 'Vancouver']; True; then ['Surrey', 'Richmond', 'Burnaby'].
cities[0]Zero-based positions count from the start.
cities[-1]Negative positions count backward from the end.
cities[0:2]The stop boundary is excluded.
"Surrey" in citiesAnswers whether an equal item is present.
| Operation | Intent | Important result |
|---|---|---|
append(x) | Add one item at the end. | Mutates the list. |
extend(items) | Add every item from an iterable. | Mutates the list. |
insert(i, x) | Insert one item before position i. | Mutates the list. |
remove(x) | Remove the first equal value. | Raises ValueError if absent. |
pop(i) | Remove and return an item, defaulting to the last. | Mutates and returns a value. |
clear() | Remove all items. | Mutates the list. |
sort() | Sort the same list in place. | Returns None. |
sorted(items) | Produce a new sorted list from an iterable. | Leaves the original list unchanged. |
Choose a deterministic case, predict the result first, then reveal the state transition. Nothing is saved.
| Mistake | Why it happens | Fix |
|---|---|---|
Reading items[len(items)] | The last valid positive index is one less than the length. | Use items[-1] or check the boundary before indexing. |
Using append([3, 4]) when you meant two items | append adds its argument as one item. | Use extend([3, 4]) when you intend to add each element. |
items = items.sort() | list.sort() changes the list in place and returns None. | Call items.sort() separately, or assign sorted(items). |
| Removing items from the same list while iterating over it | Indexes shift while the loop is deciding what comes next. | Build a filtered list, iterate over a copy, or choose another explicit strategy. |
b = a then expecting independent lists | Both names refer to the same list object. | Use a.copy() or a slice for a shallow copy when that is the intended level of copying. |
| Assuming a shallow copy duplicates nested objects too | The outer container is copied; nested references are still shared. | Model the nested structure explicitly and use deeper copying only when the data semantics require it. |
[10, 20, 30]. Predict items[-2] and items[1:3], then verify.append([40, 50]) and extend([40, 50]) produce different shapes.sort() and once with sorted(). Record the returned value and the final original list in each case.Lists are a foundation for later tuples, sets, comprehensions, functions, iteration, and many interview problems. Use the explorer until you can explain mutation and aliasing without guessing.
The current public Python assessment still serves exactly 25 distinct randomized questions from its validated bank. List-specific candidates remain preparation-only until duplicate and editorial review is complete.
List semantics on this page are anchored to the official Python documentation. The visual and List State Explorer are original KuveSu learning aids.