FAQ: Code Challenge: Loops - Odd Indices


This list definition should not be inside the loop, but before it.


ohh! could you give me an example of when the variable definition would go in the loop? Thank you


When it precedes an inner loop, otherwise, practically never. All initialization should take place outside of the loop, and only change inside the loop.


Sorry for my stupid question but I am a bit lost, what is meant with odd index? I already googled it but coudlnt find an answer where it was clear to me.

I think I got it… found an explanation here https://stackoverflow.com/questions/20268195/what-does-index-mean-in-python


What do you mean with:
step => any value to increment by ?

I dont understand why there is the 2 at the end in range?


That’s the step value. We start on index 1 and progress in steps of two so we land on all the odd indices.


I’m printing out the index numbers themselves here instead of the actual values. Is there an elegant solution to this?

[index for index in range(0, len(lst), 2)]

That is the even indices since you started at zero. It should start at 1.

return [lst[x] for x in range(1, len(lst), 2)]

will return the expected values.


Hi is there a way to check what position an index has in a list? i was trying to do it with %2 != 0 method


We can use the .index() method if we are sure there is but one item, and no duplicates since this method will only return the index of the first item it finds.

>>> a = [2,5,8,9,3,2,5,8]
>>> a.index(8)

Unless we spin off a shortened slice, we will not get to the other 8.

>>> b = a[a.index(8)+1:]
>>> b.index(8)

We do have a tool that we can use to find all the indices, but it will take some reckoning…

>>> c = [i for i, x in enumerate(a) if x == 8] # i is the index, x the item
>>> c
[2, 7]

but what if the list was [2,1,4,3,6,7]…then this wouldn’t work? we would get the even numbers back, wouldn’t we?


Yes, but they are all at odd indices.


Below is my solution. One question on the While loop.

If list length is 10, and “While i > 1” with each loop decrementing i = len(list), how many loops should I get?

I count 9, thus missing the last loop for the list length?

def odd_indices(lst):
  i = len(lst)
  empty_list = []
  while i > 1:
    for num in lst:
      if i % 2 == 0:
        i = i - 1
        i = i - 1
  return empty_list

You only enter the ‘while’ loop one time. The ‘for’ loop does all the work, and when it is finished, i is zero, the list, empty_list, is filled, and control passes back to ‘while’, whose operand i > 1 now returns False, so control passes to the return statement.

You don’t need ‘while’ at all!


Hi guys,

I tried this solution. It actually returns the correct result, but for some reason Codeacademy doesnt accept it, telling me “index is out of range”. What does this mean?

def odd_indices(lst):
list =
i = 0
while i<len(lst):
i += 1
if i%2 == 0:
return list


There is a final value for i that is equal to len(lst which puts that index out of range. Try,

while i < len(lst) - 1: