What is the format for `range` and what is an odd index?

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

>>> 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
2 Likes

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

1 Like

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

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.

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.

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.

Sorry for my stupid question but I am a bit lost, what is meant with odd index? I already googled it but coudlnt find an answer where it was clear to me.

I think I got it… found an explanation here https://stackoverflow.com/questions/20268195/what-does-index-mean-in-python

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