FAQ: Code Challenge: Loops - Odd Indices

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

Yes! And every time the function runs, empty_lst (no longer empty!) becomes longer. This is a great example of using global variables and the dangers that lurk therein.

1 Like

Here it is my solution to the exercise. I have to admit that I struggled a bit, but then I took my time to really think about the functioning of the methods I saw until now.
I was really happy when I run the code and I saw that it worked. My first authentic satisfaction since I started this course :slight_smile:

def odd_indices(lst):
  new_lst = []
  for i in list(range(len(lst))):
    if i % 2 != 0:
      new_lst.append(lst[i])
  return new_lst
1 Like

@crazybeetle, it’s a great feeling, isn’t it?

One thing - you do not need to apply the function list() to range(). The for operator does that work for you: it will either march through each element in a list, or if the object is an iterator, (technically, range() is not an iterator, but close enough for this discussion!), it will grab out each value yielded by the iterator, one at a time.

2 Likes

Yeah I realized that looking at other people’s solutions, before I posted mine.
But I posted my original solution so, hopefully, I 'd have “attracted” more explanations like…this one.
Thank you very much! :+1:t2:

1 Like