How can I obtain just the odd indices of a list?

I was wrong in my initial reply, you are correct, 6 would be an even index.

  1  2  3  4  5  6  7     // odd length
[ 3  5  8  13 21 34 55 ]
  0  1  2  3  4  5  6     // even index

You are correct, been awake too long.

def odd_indices(lst):
  new_list = []
  for i in lst:
    if i % 2 == 1:
      new_list.append(i)
      
  return new_list

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

My code is return [3,7,11]. Can someone please help?

As the name would indicate, we are seeking the odd indices, not the odd values. Our function should return the values at the odd indices.

def odd_indices(lst):
  lit = []
  i = list(range(len(lst)))
  for n in i:
    if n%2!=0 and len(i)>0:
      lit.append(lst[n])
  return lit

I did everything so long-winded. Would this technically be more efficient, though? Instead of constantly parsing range(1, len(lst), 2) for an index, I just gave it the minimum information required.

Since you are using range, then it would be expedient to let it be the one to control the steps.

range(1, len(lst), 2)

will produce a range, [1, 3, 5, ...] so that we do not have to test the parity of the index.

for i in ...:
    result.append(lst[i])

Have you studied the list slice method, yet? That will give an instant solution with no iteration.

10 posts were split to a new topic: My function appears to produce the right result but isn’t accepted

Can a while loop be used in this exercise? If so, how?
Thanks in advance.

Hello, @jaxenjoliellc.

Can a hammer & screwdriver be used to open a can? A while loop is best suited for repeating a series of steps while a specific condition exists. What would be the condition in this exercise? You could say the condition is having a valid index assigned to a variable:

lst = [1,2,3,4]
i = 0
while i < len(lst):
  #do stuff with lst[i]
  i += 1

There are, however, better tools in Python for iterating through a list. Just like a can opener is a better tool for opening cans.

1 Like

Nice analogies. I was thinking the element would be appended to the new list conditioned upon it having an odd index.

Thanks for responding @midlindner.

1 Like

this answer is so simple yet it was so simple, i didnt even consider this. I was trying to do some complex list comprehension. thanks dude

Yeah very strange I had this code and it kept returning three but when I saw the solution it gave me the right answer. I undid the solution to go back and mine kept still being incorrect. After I tried a couple new ways to make sure I understood the concept and when I went back to my original code after commenting it out it worked fine.

would you bet money on that? can you really say that if you don’t know what it does?
what do you have to indicate that it solves the problem? that it passed like 5 tests? there’s an infinite number of possible inputs, testing 5 things doesn’t prove it correct

someone who can read and therefore understand it could easily construct input for which it fails
you can also easily find failing cases by creating random input and comparing to a correct implementation

usually when you say something is right it’s because you know what it is and can reason about that it’ll do the right thing for all possible scenarios, so you would spend some time coming up with a strategy, and write that in code, and then you might be able to argue for why it would do the right thing

If I comment these lines :

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

console print extra None why this happen ?

3
10
-2
None

if I uncomment :

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

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

Console no more print None

3
10
-2
[3, 10, -2]

I could not answer why so I asked it .

None is the absence of a return value.

2 Likes

Dude, this is the most elegant solution for this exercise. It truly demonstrate your understanding about lists. Thanks for sharing

Well, I don’t care in the slightest about whether the code is right, I’m poking at your definition of correct. Proving that something is correct is difficult, and here you have something you don’t even know what it does! It’s probably not right, even without considering anything further.

list.remove removes the first matching value found, so you can create input where it will end up removing something that was originally at an odd index by also having an identical value in an even index afterwards

Thank you sir for the counter solution,
You have proven me wrong
[1,2,3,4,2,3,4] actually fails
I am very sorry if any fellow learner took my ‘solution’ as genuine
I will delete the post.

Why this doesn’t work guys?

Blockquote
def odd_indices(lst):
new_list =
for num in lst:
if num % 2 == 1:
new_list.append(lst[num])
return lst

Blockquote