FAQ: Code Challenge: Loops - Odd Indices

This community-built FAQ covers the “Odd Indices” exercise from the lesson “Code Challenge: Loops”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science

FAQs on the exercise Odd Indices

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

11 posts were split to a new topic: What is the format for range and what is an odd index?

8 posts were split to a new topic: How to use list comprehension for code challenge?

3 posts were split to a new topic: Can I use index() for the loop challenge?

4 posts were split to a new topic: Variable definition within a loop?

3 posts were merged into an existing topic: What is the format for range and what is an odd index?

2 posts were merged into an existing topic: Can I use index() for the loop challenge?

2 posts were split to a new topic: Did I loop correctly? - Code Challenge

2 posts were split to a new topic: Index out of range?

5 posts were merged into an existing topic: What is the format for range and what is an odd index?

3 posts were merged into an existing topic: What is the format for range and what is an odd index?

4 posts were split to a new topic: Using list comprehension

2 posts were split to a new topic: Code Challenge - Appending index

2 posts were split to a new topic: How could I improve this comprehension?

2 posts were merged into an existing topic: How can I obtain just the odd indices of a list?

2 posts were split to a new topic: Why do I get an index error with this code?

def odd_indices(lst):
new_list =
i = 0
while i <= len(lst):
if lst[i] % 2 == 1:
new_list.append(lst[i])
i += 1
return new_list

Why this function does not work?

That line does not test the index, but the value at that index. It should be checking if the index is odd, and capture the value, whatever it is, at that index.

if i % 2:
    n.append(lst[i])

So, I already apologize before hand if this question is super dumb since I’m 7 days into coding. But I figured out this way to solve this exercise and it does give the appropriate result, but prints an error (local variable ‘new_lst’ referenced before assignment). I don’t understand why that should be a problem, since the end result is the one desired. This is my piece of code:

def odd_indices(lst):
  substract = len(lst)
  for index in range(1, len(lst), 2):
    lst.append(lst[index])
    new_lst = lst[substract:]
  return new_lst
  

print(odd_indices([4, 3, 7, 10, 11, -2]))

I’ve seen the solution now and it is more elegant and see it is a better way to have that output, but still don’t know why the first one should be wrong.

Would anybody be nice enough to enlighten me. Thanks a lot!!