SypherTec
…
PART 3 · 06 OF 085 MIN READUPDATED 20 NOV 2026

Slicing: Taking Part of a List

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:

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

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 THREE, AS A NEW LIST · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

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:

LEAVING A NUMBER OUT · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

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:

SLICING TEXT · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

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. expenses itself 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 as expenses[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:

A SLICE AND AN INDEX PAST THE END · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

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

main.py
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:

ASKING FOR THREE, GETTING TWO · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

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:

A COMMA INSTEAD OF A COLON · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output
TypeError: list indices must be integers or slices, not tuple

With 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:

NO COLON AT ALL · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

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.

  1. Run the starting code below. Confirm your report lists every expense under Expenses:, followed by a blank line and then Expenses recorded.

  2. Find the print() that comes after your expense listing loop, above your Expenses recorded line. 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.

  3. In your new for line, change [-3:] to [:3]. Run it. Confirm the section now repeats the first three lines of your expense list instead.

  4. Change [:3] back to [-3:]. Run it. Confirm the section shows your last three expenses again.

  5. Break it on purpose: in your new for line, delete the colon inside the square brackets, so it reads for record in expenses[-3]:. Run it. Confirm the report stops after Latest three:, with TypeError: 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. So record holds 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. In record["item"], the position given is the text "item", so Python stops.

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

TRY IT YOURSELF · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

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.

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