FAQ: Code Challenge: Loops - Odd Indices

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:
  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.


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