Your report lists every expense, from the first you spent to the last. This lesson takes part of a list, so your report can also show your latest three expenses on their own.
The concept
Here is your tracker as the last lesson left it. Run it:
Your receipt lines are in the order you spent the money. So the newest expenses sit at the end of expenses. Now run this:
The first line shows three amounts in square brackets. That is a new list, holding the items at positions 0, 1 and 2. The second line shows expenses still holding all five. Taking part of a list like this is called slicing. The new list it gives back is a slice.
The two numbers are positions. The slice starts at the first one, and stops before the second. So 0:3 means positions 0, 1 and 2, but not 3. Stopping before the end position catches most people at first. When both numbers are inside the list, the end minus the start is how many items you get.
You can leave either number out. Run this:
Leave out the start, and the slice begins at the first item. Leave out the end, and it runs to the last item. A negative position counts back from the end, as expenses[-1] did in the lists lesson.
So expenses[-3:] starts three from the end and runs to the end. That is the last three items, however long the list grows.
Slicing works on text too. Run this:
A string holds its characters in order, and each one has a position, spaces included. So the same brackets take part of a string. month[:3] gave Nov, the first three characters. month[-4:] gave 2026, the last four.
The syntax
first_three = expenses[0:3]- The list's name, then square brackets, as with an index.
- Inside the brackets: where to start, a colon, and where to stop.
- The start position is included. The end position is not.
- The result is a new list.
expensesitself does not change.
Both numbers are optional, and either can be negative. On a list of five items:
expenses[:3]is the first three, the same asexpenses[0:3].expenses[3:]runs from position 3 to the end: the last two.expenses[-3:]is the last three.
The same brackets slice a string: month[:3] is the first three characters of month.
A slice is a list, so a for loop can go through it:
for record in expenses[-3:]:
print_line(" " + record["item"], record["amount"])A slice never runs off the end of a list. Run this:
The slice asked for everything up to position 10, and gave back the three items that exist. The index asked for the item at position 10, which does not exist. So Python stopped with IndexError: list index out of range.
That makes expenses[-3:] safe on a short list. With only two expenses, you get those two, and no error.
A minimal working example
expenses = [1500, 2500, 3500, 800, 4200]
month = "November 2026"
latest = expenses[-3:]
short_month = month[:3]
print(latest)
print(short_month)
print(expenses)Output: [3500, 800, 4200], then Nov, then all five amounts. Taking the slice did not change expenses.
Common mistakes
You expect the end position to be included. Run this:
No error, but only two amounts: [1500, 2500]. The slice stopped before position 2. When a slice comes back one item short, check the end number. It is the position after the last item you want, so the first three is expenses[0:3], or expenses[:3].
You type a comma instead of a colon. Run this:
TypeError: list indices must be integers or slices, not tupleWith a comma, Python reads 0, 3 as two values bundled together, which it calls a tuple. Inside a list's square brackets, Python accepts an index, which is one whole number, or a slice made with a colon. Replace the comma with a colon: expenses[0:3].
You leave out the colon. Run this:
You asked for the last three, and got one number, 3500, with no square brackets. Without a colon, expenses[-3] is an index: the one item three from the end. Only the colon makes it a slice. Inside a loop, this mistake stops your program with an error. You will meet it on purpose below.
Try it yourself
You will add a "Latest three" section to your report, under the list of every expense.
-
Run the starting code below. Confirm your report lists every expense under
Expenses:, followed by a blank line and thenExpenses recorded. -
Find the
print()that comes after your expense listing loop, above yourExpenses recordedline. Under it, add these lines:print("Latest three:") for record in expenses[-3:]: print_line(" " + record["item"], record["amount"]) print()Run it. Confirm a
Latest three:section appears under your expense list. It should repeat the last three lines of that list, in the same order. -
In your new
forline, change[-3:]to[:3]. Run it. Confirm the section now repeats the first three lines of your expense list instead. -
Change
[:3]back to[-3:]. Run it. Confirm the section shows your last three expenses again. -
Break it on purpose: in your new
forline, delete the colon inside the square brackets, so it readsfor record in expenses[-3]:. Run it. Confirm the report stops afterLatest three:, withTypeError: string indices must be integers, not 'str'. Without the colon,expenses[-3]is one record, a dictionary. A loop over a dictionary without.items()gives only its keys. Sorecordholds the first key, the text"item", not a record. On text, square brackets without a colon ask for a position, which must be a whole number. Inrecord["item"], the position given is the text"item", so Python stops. -
Fix it: put the colon back, so it reads
expenses[-3:]. Run it. Confirm your latest three expenses return, followed by the rest of your report.
Your report now shows your three most recent expenses. They are in the order you spent the money, though, not by size. The next lesson puts your expenses in order, biggest first, so you can see where most of your money went.