PCAP-31-03 practice question 3 of 25

How many elements will the list2 list contain after execution of the following snippet? list1 = [False for i in range (1, 10) ] list2 = list1 [-1:1:-1]

Full set: 147 questions
Answer options
A: zero
B: five
C: seven Correct answer
D: three
Correct Answer:
C. seven
Explanation

list1 comprehension builds nine False values (range(1,10) → 1-9). Slice list1[-1:1:-1] starts at index 8, moves backward by -1, stops before index 1. Indices included: 8,7,6,5,4,3,2 → 7 elements.

Why Incorrect

A. Slice is non-empty; seven elements selected.
B. Contains seven, not five, elements.
D. Contains seven, not three, elements.

References

1. Python 3 Library Reference, “Built-in Types — Common sequence operations”, ¶5 (docs.python.org/3/library/stdtypes.html#typesseq-common) – explains negative-step slicing rules.

2. Python 3 Library Reference, “range() objects”, ¶2 (docs.python.org/3/library/stdtypes.html#typesseq-range) – details of range(1,10) endpoints.

Scroll to Top