KKAIROS
Practical guideIllustrative3 min read

Python lists: indexes, slices and the IndexError

Lists are ordered and mutable collections; indexing starts at zero; slicing returns a new list that holds the selected items.

Published:

Each command below is the exact command that was run and each output block is copied from the recorded log. Test ids, log hashes and exit statuses are in the evidence table.

TE-30-A: an out-of-range index raises IndexError while an out-of-range slice returns a short or empty list

No setup needed: the command below creates what it uses in the throwaway folder.

Command:

python3 -c "l=[1,2];
try:
    l[5]
except IndexError as e:
    print('IndexError', e)
print('slice', l[5:9], l[-1])"

Recorded output:

IndexError list index out of range
slice [] 2

This run checks the exit status and records the output; it asserts no expected text.

TE-30-B: list.sort changes the list in place and list.reverse reverses it in place

No setup needed: the command below creates what it uses in the throwaway folder.

Command:

python3 -c "l=[3,1,2]; l.sort(); print(l); l.reverse(); print(l)"

Recorded output:

[1, 2, 3]
[3, 2, 1]

Exit status: 0. This status and the output match what was written before the run.

TE-30-C: expected non-zero exit: pop on an empty list raises IndexError

No setup needed: the command below creates what it uses in the throwaway folder.

Command:

python3 -c "[].pop()"

Recorded output: none, standard output was empty.

Standard error from the same run:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
IndexError: pop from empty list

Exit status: 1. This status and the output match what was written before the run.

Reading the recorded output

The first output line comes from the except branch: l[5] raised IndexError, and the message is list index out of range. The second line shows two values. l[5:9] gave [], an empty list, and no error. l[-1] gave 2, which is the last item of the list [1, 2]. The run does not print an exit status, and it does not run a slice that overlaps part of the list.

TE-30-B: After l.sort(), print(l) showed [1, 2, 3], so the list itself was sorted. l.reverse() then made it [3, 2, 1]. TE-30-C: pop on an empty list exited with status 1. The last line of standard error is IndexError: pop from empty list.

Ad

Limits

Out-of-range index raises IndexError; slices tolerate out-of-range bounds; negative indexes count from the end.

Source

Evidence

Evidence table

| Test | What it checks | Exit | Verdict | Log sha256 (first 12) | |---|---|---|---|---| | TE-30-A | an out-of-range index raises IndexError while an out-of-range slice returns a short or empty list | 0 | PASS | adbb108d51b2 | | TE-30-B | list.sort changes the list in place and list.reverse reverses it in place | 0 | PASS | cb889e75ef57 | | TE-30-C | expected non-zero exit: pop on an empty list raises IndexError | 1 | PASS | cd4a89cf49fd |

Every row has a recorded log holding the setup commands, the command, stdout, stderr, exit status and the final directory state. The first 12 characters of the log's sha256 are in the table.

All full guides: All guides