KKAIROS
Practical guideIllustrative3 min read

Python List Slicing: take part of a list

a[start:stop] returns a new list from start up to but not including stop. a[i:j:k] takes a step of k; in this run a[::-1] returned the items in reverse order. a[:] is a shallow copy: the new list is separate, but nested mutable items are shared with the original.

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-78-A: slice bounds, reverse slice, and a [:] copy that is separate from the original

Setup for this run, inside a throwaway directory (mktemp -d):

cat > s.py <<'EOS'
a = [10, 20, 30, 40]
print(a[1:3])
print(a[::-1])
b = a[:]
b[0] = 99
print(a[0], b[0])
EOS

Command:

python3 s.py

Recorded output:

[20, 30]
[40, 30, 20, 10]
10 99

Exit status: 0. The output matches the expected text written before the run.

TE-78-B: a negative start, a step of 2 and a slice assignment on a list

Setup for this run, inside a throwaway directory (mktemp -d):

cat > s.py <<'EOS'
a = [10, 20, 30, 40]
print(a[-2:])
print(a[::2])
a[1:3] = [0]
print(a)
EOS

Command:

python3 s.py

Recorded output:

[30, 40]
[10, 30]
[10, 0, 40]

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

TE-78-C: expected non-zero exit: a single index past the end raises IndexError, while a slice past the end does not

Setup for this run, inside a throwaway directory (mktemp -d):

cat > s.py <<'EOS'
a = [10, 20]
print(a[5:9])
print(a[9])
EOS

Command:

python3 s.py

Recorded output:

[]

Standard error from the same run:

Traceback (most recent call last):
  File "/tmp/b21-tnaoeuy8/work/s.py", line 3, in <module>
    print(a[9])
IndexError: list index out of range

Exit status: 1. This status and the output match what was written before the run. The path in the traceback line is a temporary folder and differs on each machine and each run.

Reading the recorded output

The first line, [20, 30], shows that a[1:3] starts at index 1 and stops before index 3, so the item at index 3, which is 40, is left out. The second line shows all four items in reverse order. In the third line, b is a copy made with a[:]. After b[0] was set to 99, a[0] still read 10 and b[0] read 99, so assigning a number in the copy did not change the original. The run used only numbers, so it does not show what happens when the list holds other lists.

TE-78-B: a[-2:] gave [30, 40], a[::2] gave [10, 30], and assigning [0] to a[1:3] left [10, 0, 40]. TE-78-C: by the setup, the list has two items. The slice a[5:9] printed [] and the single index a[9] stopped the program with exit status 1. The last line of standard error is IndexError: list index out of range.

Ad

Limits

Only lists of numbers were tested. Nested mutable items were not run. The docs state the step rule, and the reversed order is what TE-78-A shows.

Source

Evidence table

| Test | What it checks | Exit | Verdict | Log sha256 (first 12) | |---|---|---|---|---| | TE-78-A | slice bounds, reverse slice, and a [:] copy that is separate from the original | 0 | PASS | 96257f718983 | | TE-78-B | a negative start, a step of 2 and a slice assignment on a list | 0 | PASS | 807c660edf3a | | TE-78-C | expected non-zero exit: a single index past the end raises IndexError, while a slice past the end does not | 1 | PASS | 3812f9dd4fe7 |

Recorded on: bash 5.1.16(1)-release; git version 2.34.1; Python 3.10.12. Locale C.UTF-8, TZ=UTC. Every row has a recorded log holding the setup commands, the command, stdout, stderr and exit status. The first 12 characters of the log's sha256 are in the table.

All full guides: All guides