PCEP-30-02 · Question #17
How many elements does the L list contain? ``python 1 L = [i for i in range(-1, -2)] ``
The correct answer is D. zero. range(-1, -2) produces an empty sequence because the default step is +1, meaning it would try to count upward from -1 toward -2 - but -1 is already greater than -2, so there are no valid values to yield. The list comprehension iterates over nothing, leaving L = [] with zero…
Question
1 L = [i for i in range(-1, -2)]
Options
- Aone
- Btwo
- Cthree
- Dzero
How the community answered
(45 responses)- A2% (1)
- B4% (2)
- C11% (5)
- D82% (37)
Explanation
range(-1, -2) produces an empty sequence because the default step is +1, meaning it would try to count upward from -1 toward -2 - but -1 is already greater than -2, so there are no valid values to yield. The list comprehension iterates over nothing, leaving L = [] with zero elements.
Why the distractors are wrong:
- A (one): A common trap - you might assume
-1is included since it's the start, butrange()only yieldsstartwhen the sequence can actually progress towardstop. - B (two): There's no scenario where both
-1and-2are included;range()never includes the stop value. - C (three): Completely unfounded - no values in
[-1, -2)with step+1exist.
Memory tip: Think of range(start, stop) as "count up from start as long as the value is still less than stop." If start >= stop with the default step, you get zero elements - the range is already "past the finish line" before it begins.
Community Discussion
No community discussion yet for this question.