Skip to lesson content
navan✳THE BUILD LABPYTHON 3
0/101 complete
BUILD LAB / PYTHON/ Organise the chaos
EXPERIMENT 10 / 101

Your first collection.

Keep related items in order, then add, remove, and explore them.

BY NAVAN SAMI◷ 6 MIN READPYTHON 3

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.

lists.py
backlog = ["prototype", "review"]
backlog.append("ship")

print(backlog[0])
print(backlog[-1])
print(backlog[:2])
print(len(backlog))
↳ EXPECTED OUTPUT
prototype
ship
['prototype', 'review']
3

03 Under the hood

  1. 1

    append() adds one item to the end. The list now has three elements, but its first index is still zero.

  2. 2

    backlog[-1] gives the last item. backlog[:2] gives a new list containing the first two items.

  3. 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

A SMALL MISSION

Build a list of three tools. Remove the last one with pop(), then print the remaining list.

Reveal one possible solution +
one_possible_solution.py
tools = ["editor", "terminal", "browser"]
tools.pop()
print(tools)

05 Check your signal

QUICK SIGNAL CHECK

Which index accesses the first list item?

THAT’S ANOTHER IDEA IN YOUR TOOLKIT.

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 ↗.

Built with curiosity. By Navan Sami. ↗Python 3 · Think. Try. Build.