0

I'm trying to figure out why some statements require and RETURN and others do not? Like below, I wrote this and l1.append(val) doesn't need a return but for example l1[val] does?

Does it just depend on the variable?

L = [1,3,5,7,9,11, 2] 

print 'Original List =', L
def change_list(l1, val, decision):
    if decision == 'append':
        return l1[val]
    elif decision == 'ret':
        l1.append(val)
    elif decision == 'instance':
        return l1.count(val)
    elif decision == 'sort':
        l1.sort()
    return l1
res=change_list(L, 2, 'append')
print 'Output =', res

However if I change it to the following (remove the return) it breaks! Wh does this happen given some of the conditions need a return to work, some do not?:

L = [1,3,5,7,9,11, 2]

print 'Original List =', L
def change_list(l1, val, decision):
    if decision == 'append':
        l1[val]
    elif decision == 'ret':
        l1.append(val)
    elif decision == 'instance':
        return l1.count(val)
    elif decision == 'sort':
        l1.sort()
    return l1
res=change_list(L, 2, 'append')
print 'Output =', res
Phil
  • 47
  • 9
  • This link should answer your question: http://stackoverflow.com/questions/15300550/python-return-return-none-and-no-return-at-all – Hektor Mar 23 '16 at 19:52
  • it depends on your purpose, what you gonna do with it – Haifeng Zhang Mar 23 '16 at 19:55
  • What do you mean it does not need a return? If your `decision == 'ret'` condition is met, you are appending to li and then you are returning `li`. So I do not see where you are drawing your conclusion from. – idjaw Mar 23 '16 at 19:55
  • @idjaw if i remove the return from the "return l1[val]" - and change the input of the function at bottom to "append" it doesn't work? This is why I'm confused that .append doesn't need to have a return before it but l1[val] does! – Phil Mar 23 '16 at 20:08
  • @Phil Sorry, I have no idea what you are trying to explain. I would suggest editing your question to give more explicit examples. Maybe it's just me, but I'm having a hard time following what your problem is. – idjaw Mar 23 '16 at 20:11
  • @idjaw sorry my bad! examples coming! – Phil Mar 23 '16 at 20:13
  • Edited the question! Hopefully clearer now! – Phil Mar 23 '16 at 20:17
  • @Phil Now please explain what "it breaks" means. :) – idjaw Mar 23 '16 at 20:21
  • @idjaw the output went from the single digit (correct) to returning the full list instead (given I've asked it to return the full list at the end)! – Phil Mar 23 '16 at 20:24
  • oh......you only need to include the return when the list is being returned I think i.e. append, sort but NOT count, [1] etc.? – Phil Mar 23 '16 at 20:26

1 Answers1

0

The reason for this is that l1.append(val) affects the original object. As in, calling .append(val) goes to the array that you passed to your change_list function and does it to your original L array. Where as l1[val] only asks your original L for the value. So it must be returned

Luis F Hernandez
  • 891
  • 2
  • 14
  • 29