CYBER WEEK - EXTRA SAVINGS EVENT

# Lists - Practice Problem #3 - Step (Why my function is out of range)

My Code:

def skip_two(some_list):
result = []
for i in range(1, 12, 3):
result.append(some_list[i])
return result

What I expected to happen seems to be the right answer (I applied my function to the list in the example section, and it returned the same result).

However, the feedback after submitting the answer was shown as below:
Executing the function skip_two caused an error: IndexError: list index out of range .

I don’t get it… I mean the range(1, 12, 3) will count from 1, increment by 3, and stop right before 12 (which is 11). Why would it be out of range…? Please help.

Thanks in advance!

We don’t expect you solve the problem for only the example we provide, otherwise you could just do. . .

def skip_two(some_list): return ['b', 'e', 'h', 'k']

We expect you to provide a solution that solves the problem as described.

What happens with your solution to an input like ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k']?

Hi Bruno,

When I simply ran the code without submitting the answer, my function returned the output [‘b’, ‘e’, ‘h’, ‘k’].

As you said, I do understand that DQ does not expect me to solve in one single way; however, the feedback message said the index is out of range, so I figured my code/logic has some problems.

If the code is considered correct as long as the output is [‘b’, ‘e’, ‘h’, ‘k’], then I’m good here.

Thank you.

Really? That’s not supposed to happen. Can you share a screenshot of this happening? Do note my example input from before:

Here is the screenshot of my solution for your reference.
Thanks!

That’s a different input than what I asked.

Alright. I changed my input as you guided, but the output of running without submitting is still the same – [‘b’, ‘e’, ‘h’, ‘k’]

Then, since the range limit of the for loop “12” is greater than the length of the list_1, so the code is supposed return an error message. Is that what you mean?

Apologies! You are correct. I confused myself.

This is a bug on our end. Your answer is correct.

Got it. Thanks for the prompt feedback!

Different solution, same error.

Different solution, but no error:

def skip_two(lyst):
return lyst[1:12:3]
1 Like