SypherTec
…
PART 1 · 06 OF 074 MIN READUPDATED 19 NOV 2026

Lists: Many Values Under One Name

Your tracker has one variable per expense. This lesson moves your expenses into a single list, so a new expense no longer needs a new name.

The concept

Here is your tracker as the last lesson left it. Run it:

WHERE YOU LEFT OFF · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

Four names, and a total line that names each one. Now run this:

THREE AMOUNTS, ONE NAME · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

The first line printed all three amounts at once, inside square brackets. They are stored under one name, expenses, in the order you wrote them. A value that holds other values in order is a list. The second line printed 3: len() is a built-in function that counts the items in a list.

You can pick one item out of a list by its position:

PICKING ITEMS BY POSITION · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

expenses[0] gave 1500, the first item. Python counts positions from 0, not from 1. This trips up almost everyone at first. In a list of three items, the positions are 0, 1 and 2.

The last line used -1. A negative position counts from the end, so expenses[-1] is always the last item, however long the list is.

A list can grow:

ADDING AN ITEM TO THE END · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

expenses.append(3500) added 3500 to the end of the list. The list now has three items, and len agrees.

append is written after the list's name and a dot. A function that belongs to a value like this is called a method. Here, append belongs to the list and changes that list.

The syntax

expenses = [1500, 2500, 3500]

Square brackets hold the items. A comma separates each item from the next.

first = expenses[0]
last = expenses[-1]
count = len(expenses)
expenses.append(4200)
  • expenses[0] is the item at position 0. The number in square brackets is called the index.
  • expenses[-1] is the last item.
  • len(expenses) gives back how many items the list holds.
  • expenses.append(4200) adds one value to the end. Write it on a line of its own.

A minimal working example

main.py
expenses = [1500, 2500, 3500]
expenses.append(4200)
count = len(expenses)
 
print(expenses)
print(f"Expenses recorded: {count}")

Output: [1500, 2500, 3500, 4200], then Expenses recorded: 4.

Common mistakes

You ask for a position past the end. Run this:

A POSITION THAT DOES NOT EXIST · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output
IndexError: list index out of range

Three items sit at positions 0, 1 and 2. Position 3 does not exist. When you see this error, count from 0 and check the last position the list really has.

You leave out a comma between items. [1500 2500] gives SyntaxError: invalid syntax. Perhaps you forgot a comma? This time Python's guess is right. Add the comma.

You use round brackets to pick an item. Run this:

ROUND BRACKETS INSTEAD OF SQUARE · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output
TypeError: 'list' object is not callable

Round brackets after a name mean "call this", the way print(...) calls print. A list is not something you can call. Use square brackets to pick an item: expenses[0].

You store what append gives back. expenses = expenses.append(4200) looks reasonable, but print(expenses) then shows None. append changes the list and gives back nothing, and None is Python's word for nothing. That line replaced your list with None. Write expenses.append(4200) on its own line.

Try it yourself

You will move your expenses into a list without changing your total.

  1. Run the starting code below. Write down your total.
  2. Under the fuel = int(fuel_receipt) line, add expenses = [1500, 2500, 3500]. Use your own bus_fare, lunch and data_bundle amounts, in that order. Add print(expenses) as the very last line of the file. Run it. Confirm the last line shows your three amounts in square brackets.
  3. Under the expenses line, add expenses.append(fuel). Run it. Confirm the list now has four amounts, with your fuel amount last.
  4. Change your total line to total = expenses[0] + expenses[1] + expenses[2] + expenses[3]. Then delete the bus_fare, lunch and data_bundle lines. Run it. Confirm your total matches the one you wrote down.
  5. Under the append line, add count = len(expenses). Above your Total spent line, add print(f"Expenses recorded: {count}"). Delete the print(expenses) line. Run it. Confirm the report shows Expenses recorded: 4 above your total.
  6. Break it on purpose: in your total line, change expenses[3] to expenses[4]. Run it. Confirm the console shows IndexError: list index out of range, pointing at your total line.
  7. Fix it: change the 4 back to 3. Run it. Confirm your report returns.
TRY IT YOURSELF · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

Your expenses live in one list now. The total line still names every position by hand, though, so a fifth expense would be left out of the total. The next lesson replaces that line with one function that reads the whole list.

QUIZ · 3 QUESTIONSCheck what stuckStart the quiz
PART 1 · 06 OF 07❮❯