FAQ: Code Challenge: Loops - Odd Indices


#1

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!


#2

In this part of the code “for index in range(1, len(lst), 2)”, the part about len(lst) in the range seems to be not understandable to me.


#3
range (start, end, stride)

start => inclusive
end   => exclusive
step  => any value to increment by
>>> lst = [9,3,1,8,7,4,5,6,7,2,8,4,7,9,2,3,4]
>>> len(lst)
17
>>> for index in range(1, len(lst), 2):
  print (lst[index], end=' ')

  
3 8 4 6 2 4 9 3 
>>> 

#4

But shouldn’t the odd index be 3, 7 and 11?


#5
>>> range(1, len(lst), 2)
range(1, 17, 2)
>>> list(range(1, len(lst), 2))
[1, 3, 5, 7, 9, 11, 13, 15]
>>> 

Notice all the indices are odd?

3 8 4 6 2  4  9  3    =>  value
1 3 5 7 9 11 13 15    =>  index

#6

Ok, got it. Tq u very much for the instant reply to my question.


#10

a bit confused, how can i use a list comprehension here? don’t think ive done it correctly.

my code so far:

def odd_indices(lst):
new_lst = [lst[i] for i in lst if (i % 2 == 1) and (0 <= i < len(lst))]
return new_lst


#11

I don’t think list comprehension is to blame for anything there. If a regular loop is more comfortable, you might want to do that first while you sort out the logic. At the very least it’s easier to insert print’s to write out what’s being done step by step so that it’s easier to compare to how you would do it manually.


#12

i see, i initially attempted the exercise with a list comprehension, and upon encountering difficulty i did end up using a regular loop, with success. was just curious about how it could be possible with list comprehension. i’ll play around with it and figure it out.
thanks!


#13

They’re not really any different


#14

true, a list comprehension is just a more concise for loop for lists right?


#15

They avoid some boilerplate for a couple common patterns, and because they’re expressions they fit in more places.
Filter and map are also very common operations with some programmers, and comprehensions are often preferred over using filter and map themselves.

def add100(x):
    return x+100

map(add100, range(3))  # 100 101 102
[x + 100 for x in range(3)]  # 100 101 102

#16

ahh ok, so they’re more useful in certain situations.
thank you for your time!


#17

Thanks Roy, good explanation !
I don’t understand why in my following code: the for loop returns only the the first number (3) and stop???

def odd_indices(lst):
new_lst =

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


#18

Sounds like the return statement may be nested in the loop, which will return on the first iteration. Be sure the line is outside of the loop body.


#19

Yes it was that, thanks :slight_smile:


#20

the solution to this challenge was quite straight forward, but i did it the stupid way using .index():

#Write your function here
def odd_indices(lst):
  new_lst = []
  for item in lst:
      if lst.index(item)%2==1:
        new_lst.append(item)
  return new_lst

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

#21

No way is ‘stupid’ though some do come with provisos and gotchas. list.index() has one of its own. What if there are duplicates in the list? .index() will only ever find the first one. In this exercise we wouldn’t need to know the index in this way.

We know that a list has a sequential index starting at 0. Zero is even so we would want to start one space over, with 1. Next we go to 3, then 5, then 7, and so on, in steps of 2. Sounds like a job for range since it can be started and stopped anywhere, and we can step in any stride length.

range(1, len(lst), 2)

After that it’s just a matter of appending the value at the current index to the return list. No if's about it.


#22

when you look at it from that perspective , it makes complete sense!
much love for the detailed explanation <3


#24

I am confused as to why this doesn’t work for me. When I run the code it only prints the last index that’s negative When I print(odd_elements) it prints the proper output.

def odd_indices(lst):
  for odd in range(len(lst)):
    odd_elements = []
    if (odd % 2 == 1):
      odd_elements.append(lst[odd])
  return odd_elements