0
def f(i):
    return i + 2
def g(i):
    return i > 5
def applyfg(L,f,g):
    for i in L:        
        # This is the part i'm having trouble with
        if g(f(i))== False:
            print (i)            
            L.remove(i) 



L=[0,-12,-3,-17]
print(applyfg(L, f, g))
print(L)

For some reason I don't understand this line here doesn't seem to apply the functions *correctly. It should apply f an then g to each item in the list to basically remove any that don't return True.

 if g(f(i))== False

*incorrectly

Drs.Arale
  • 1
  • 1
  • "It should apply f an then g to each item in the list to basically remove any that don't return True." What part of that do you not understand? –  Oct 03 '16 at 01:10
  • @user2357112 I don't think this is a duplicate: the OP is having problem with understanding the if condition. It's not (yet) about what happens to the list when removing elements while iterating over it. –  Oct 03 '16 at 01:10
  • @Evert: The `if` condition looks okay (though awkward) to me. – user2357112 Oct 03 '16 at 01:30
  • @user2357112 that's not what I'm saying: I'm saying the OP has a problem *understanding* that `if` condition. Hence the question is (quite) different than its suggested duplicate. –  Oct 03 '16 at 01:33
  • @Evert: It looks to me like the questioner's understanding of the `if` condition is fine, but they just think they have it wrong because they're blaming the error they're getting on the wrong aspect of their code. – user2357112 Oct 03 '16 at 01:35
  • @user2357112 Hence my first comment as a question to the OP: I'd first like to understand what precisely is the problem, before possibly closing it as a duplicate. You've now assumed something, and while you may be correct, I think the closure for now is premature, at least for this reason. "Unclear what you're asking" is then better. –  Oct 03 '16 at 01:41
  • Okay, i'll try to explain myself better. I want the code there to iterate thorugh the list and check if g(f(i))) return True. if it does it should remove that i from the list. I just checked out the topic I'm a duplicate of and that answers my oddly asked question. I didn't think that removing from a list shifts the list by one spot. – Drs.Arale Oct 03 '16 at 09:59

0 Answers0