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)
2
>>> 

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

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

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
        
      else:
        empty_list.append(num)
        i = i - 1
        print(empty_list)
  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:
continue
list.append(lst[i])
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:

Hi I wrote the following code which returns the right values but doesn’t seem to count as the correct answer. Is this a bug or is there something inferior about my coding?

#Write your function here
empty_lst = []
def odd_indices(lst):
  for i in range(len(lst)):
    if i % 2 != 0:
      empty_lst.append(lst[i])
  return empty_lst
#Uncomment the line below when your function is done
print(odd_indices([4, 3, 7, 10, 11, -2]))

Here is the codeacademy solution:

#Write your function here
def odd_indices(lst):
  new_lst = []
  for index in range(1, len(lst), 2):
    new_lst.append(lst[index])
  return new_lst

#Uncomment the line below when your function is done
print(odd_indices([4, 3, 7, 10, 11, -2]))

To solution given by the author leverages the range function’s ability to start and stop anywhere, and to proceed at any stride length from one element to another in a numeric sequence starting at start.

Above, starts on the first positive odd number, and then creates a range with difference of 2 between all the numbers. That range comprises all the odd indices in a list, so can be used to return a list of only the elements it can poll.

This is pretty interesting.

Call your function, exactly as you wrote it, twice, like this:

#Write your function here
empty_lst = []
def odd_indices(lst):
  for i in range(len(lst)):
    if i % 2 != 0:
      empty_lst.append(lst[i])
  return empty_lst
#Uncomment the line below when your function is done
print(odd_indices([4, 3, 7, 10, 11, -2]))
print(odd_indices([4, 3, 7, 10, 11, -2]))

What do you get??
What’s going on?

Hint: Try it with empty_ list defined inside the function, like the “solution.”

1 Like

I see. empty_lst is being saved outside the function.

1 Like