FAQ: Code Challenge: Loops - Odd Indices

Hi, was trying the other way round but when it run it returns me with [3]
any idea what is the problem and what should i change?
new to programming tho

The reurn statement is used to return some object references back to the main code block. It immediately exits the function when called. Why might this alter any output from your function?

Hi, I tried another way to solve the problem but I do not understand why my code does not work. Can someone help please? The function takes the first number only but I do not understand why.

#Write your function here
def odd_indices(lst):
odd_indices =
for i in lst:
#print(i)
if i % 2 == 0:
odd_indices.append(i)
return(odd_indices)

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

It’s hard to tell without formatting but it sounds like an indentation error where something is returning sooner than it should :wink:
In the future you can find details of formatting and other tricks at the following FAQ-

Be aware than i in lst provides the elements of the list, not their indices and note the output of i%2.

Thanks for your answer. Sorry that I for got the formatting.
The formatting is not the problem.
When I run the function the output is just for.
It seems as if the if-loop does not check all items in (lst).

I meant that it’s hard for anyone reading your code to tell exactly what’s wrong. Indentation is used to group code blocks together in Python so I expect it’s an indentation issue which is causing your function to return early.

A print statement or several in the loop would help you to determine how many times your loop actually gets performed. It’s also an easy way to determine what happens at each step which makes it easier to diagnose problems that you cannot spiot by reading the code.

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

That is the code as I have entered it.
Output from print is “[4]”.

Does that help?
Sorry for the irritations, I am new :slight_smile:

The length of that returned list is due to the indentation. Remember that return exits a function once executed. Perhaps adding the following line to your code will help you to see what’s happening-

def odd_indices(lst):
    odd_indices = [ ]
    for i in lst:
        # using a proper counter might be nicer but this example is short enough not to
        print(i)  # continue from here as before

It’s also worth noting that the value returned is not at an odd index (note that Python indexing begins at zero). My previous comments might help you work out why.

1 Like

Why do I receive only part of the answer (and therefore an incorrect answer) with my attempted code?

#Write your function here
def odd_indices(lst):
newlist =
for num in lst:
newlist.append(lst[1])
lst.remove(lst[0])
lst.remove(lst[0])
return newlist

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

I get 3 and 10, rather than 3, 10 and -2.

It’s generally a bad idea to alter the object you’re iterating over since it so often results in unusual behaviour. If you wanted to work out what’s going on, why not throw in some print statements after each step to see what’s changing.

It’d also be worth having a quick look through this guide on code formatting as it makes code much easier to read-

On a more general note I’d be very careful about altering global objects inside a function at any time. It’s rarely necessary and if the main purpose of your funciton is to do so make sure it’s very obvious this is the case since it can obscure what’s happening for whoever looks at the code (which can often include yourself at a later date).

1 Like

A shorter version

def odd_indices(lst):
new_list = [lst[i] for i in range(len(lst)) if i % 2 != 0]
return new_list

1 Like

This would also work:

def odd_indices(lst):
  new_list = [lst[i] for i in range(1, len(lst), 2)]
return new_list

When we use range we don’t need a comprehension, just cast the range object as a list.

list(range(...))

As a comprehension we can use the spread operator…

[*range(...)]
1 Like