The chalkboard menu changes with the season, and erasing the wrong line costs a morning. A list keeps the order, but adding and removing need care. What does the menu look like after the pumpkin drink lands in the middle?
A list stores an ordered sequence of values. Lists are mutable, so you can add, remove, and reorder items. Indexes start at 0, and slices let you copy a range without changing the original. You will also compare aliases with copies and choose between pop and remove.
Read by index
Use square brackets with an integer position. The first item is at index 0. Negative indexes count from the end: -1 is the last item. An index outside the list raises IndexError.
orders = ["latte", "mocha", "tea"]
print(orders[0])
print(orders[-1])
print(len(orders))Indexing and length
Add, remove, and slice
append adds one item to the end. remove deletes the first matching value. A slice copies a range: items[1:3] returns indexes 1 and 2. The ending index is excluded.
orders = ["latte", "mocha"]
orders.append("tea")
orders.remove("mocha")
print(orders)
print(orders[0:2])Changing a list
Sort and summarise
sort() changes the list in place and returns None. sorted() returns a new list. sum and len work on lists of numbers. Use reverse=True for descending order.
prices = [6.0, 4.5, 3.25]
prices.sort()
print(prices)
print(sum(prices))
print(sorted(prices, reverse=True))Ordering and totals
Slicing copies, assignment aliases
b = a copies the reference, not the list. Use a[:] or list(a) for a shallow copy. A shallow copy is enough when the items are numbers or strings; if the list contains dictionaries, the inner dictionaries are still shared.
orders = ["latte", "tea"]
alias = orders
copy = orders[:]
alias.append("mocha")
copy.append("water")
print(orders)
print(copy)
records = [{"id": 1}]
shallow = records[:]
shallow[0]["id"] = 99
print(records)The outer list is copied; inner objects are shared
Pop by position, remove by value
pop() removes by position and returns the value; remove() deletes the first matching value and returns None. Choose based on whether you need the removed item. Do not remove from a list while iterating over it; build a new list or iterate over a copy.
queue = ["latte", "tea", "mocha"]
served = queue.pop(0)
print(served, queue)
queue.remove("mocha")
print(queue)Two removal methods with different contracts
sorted() returns a new list
sorted() returns a new list; list.sort() changes the original in place and returns None. Choose based on whether the original order matters. Both accept key= for custom ordering, and reverse=True for descending order. A common bug is writing prices = prices.sort(), which replaces the list with None.
prices = [4.5, 3.0, 2.25]
ascending = sorted(prices)
prices.sort(reverse=True)
print(ascending)
print(prices)A copy versus an in-place sort
Key points
- Indexes start at 0; negative indexes count from the end.
- append changes the list; a slice returns a new list.
- The stop index in a slice is excluded.
- sort changes the list and returns None.
- b = a aliases; b = a[:] copies only the outer list.
- sorted() copies; list.sort() mutates and returns None.
Common mistakes
- Using orders[3] on a three-item list, which raises IndexError.
- Writing orders = orders.append("tea"), which replaces the list with None.
- Confusing remove(value) with pop(index).
- Removing items from a list while iterating over it.
- Assigning the result of list.sort() and losing the list.
Start with two drinks, append a third, sort the list, and print the first and last items.
Try it yourself
Change the values, predict the output, then run the code. The Python runtime is downloaded the first time you press Run, so the first run can take a few seconds.
Practice this lesson
12 questions with immediate explanations.