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

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

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!