Your first collection.
Keep related items in order, then add, remove, and explore them.
01 The idea
A list is an ordered, mutable collection. It can hold many values under one name. Indexing begins at zero, negative indexes count from the end, and slices return new lists. Methods such as append() and pop() change the original list.
02 The experiment
Read it once. Predict what will happen. Then copy it into a .py file and run it in your local Python setup.
backlog = ["prototype", "review"]
backlog.append("ship")
print(backlog[0])
print(backlog[-1])
print(backlog[:2])
print(len(backlog))prototype ship ['prototype', 'review'] 3
03 Under the hood
- 1
append() adds one item to the end. The list now has three elements, but its first index is still zero.
- 2
backlog[-1] gives the last item. backlog[:2] gives a new list containing the first two items.
- 3
Lists can be changed in place. If two names refer to the same list, both see the mutation. Use copy() for an independent shallow copy; nested objects can still be shared.
04 Your turn, builder
Build a list of three tools. Remove the last one with pop(), then print the remaining list.
Reveal one possible solution +
tools = ["editor", "terminal", "browser"]
tools.pop()
print(tools)05 Check your signal
Which index accesses the first list item?
Experiment. Logged.
When you can explain the idea and make the example your own, mark it complete. Your log stays in this browser.
Keep exploring
Go deeper with the official Python guide ↗.