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.
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 [] 2This 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 listExit 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.
Limits
Out-of-range index raises IndexError; slices tolerate out-of-range bounds; negative indexes count from the end.
Source
Evidence
TE-30-A: an out-of-range index raises IndexError while an out-of-range slice returns a short or empty list
- logs: one recorded log for each row above
- Recorded on: Linux x86_64, kernel 7.3.4; bash 5.1.16; git 2.34.1; python 3.10.12; findutils 4.8.0; grep 3.7; sed 4.8; gawk 5.1.0; tar 1.34; curl 7.81.0. Locale C.UTF-8, TZ=UTC.
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