Python range quiz question


Tripped up by this quiz question. I'm curious if anyone can explain how it would work to select every other item given these options?



Let's first look at what you chose as a response, so we can find out why it is not the solution.

Given that x is a list, we can query its length so in theory this response could work if the conditions/states are set favorably. Unfortunately, in this case they are not. No matter. It is justifiably marked wrong, no mistake.

range generated list => [ 1, 2, 3, .., (len(x) - 1) ]
iterator values when step is `-2` => 1, -1, -3, .., ?

This would set up an infinite loop.

The correct answer is the first one. It starts with the correct index value for parsing a list. The limit condition is len(x), which is of course correct. The step is positive 2, which when starting near zero is always a positive sign.

range generated list => [ 0, 1, 2, .., (len(x) - 1) ]
iterator values when step is `2` => 0, 2, 4, .., known_bound