Ah so… how silly of me, all my tests were with even length lists. Thanks for pointing that out!
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 …
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
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.
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).
A shorter version
def odd_indices(lst):
new_list = [lst[i] for i in range(len(lst)) if i % 2 != 0]
return new_list
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(...)]
Where can I get more problems like this to work on. I need to write and problem solve through more questions like this to understand it.
For now, work on understanding all the in’s and out’s of range()
. Create a whole plethora of different sequences using the one function. See if you can predict the outcome before running the code. Test yourself in this way so that you begin to spot things.
Sounds good. I find it easy to just cruise through each section. But without repetition I don’t understand it completely.
There is a reason this takes months and years to fully learn… It’s a lot to take in, there are endless nuances, hills and curves, and cliffs. We did not learn to talk in a week or a month. Treat programming along the same timeline and respect that it is not something we breeze through. Review, repeat, redo, and practice, practice, practice. When you’re not doing that, read the docs, articles about concepts, explore and experiment extensively with each new concept.
As we progress we are adding to our tool chest, granted, but we cannot add to our brain trust without a massive amount of repetition and introspection. Learn the basics backwards and forwards, up and down, left and right. Don’t overlook anything, and never assume that you are done learning.
Happy coding!
Hi, i am new to coding and i am wondering if someone can explain to me why doesn’t this code work?
#Write your function here
def odd_indices(lst):
new_lst = []
for index in lst:
if lst[index] % 2 != 0:
return new_lst.append(lst[index])
#Uncomment the line below when your function is done
print(odd_indices([4, 3, 7, 10, 11, -2]))