FAQ: Code Challenge: Lists - Remove Middle


yes, that was the post I was referring to.
I am more confused. In your example with s and list_without_middle you created a variable and stored the modified list (which is pretty much what I did in my exercise). however, in the example above that, there is no variable assignment…just the line with return. This is where I interpreted it to mean that the argument was overwritten with the new values.

Is it because return stores whatever value comes from the function, thereby “acting as a variable” and why there isn’t a local variable in the function?
I wonder if I am creating redundant variables…or maybe I am getting mixed up with tuples…see I know just enough to get myself in trouble…

1 Like

return is part of the assignment back to the caller. It doesn’t store anything, just hands it back. The caller can then either print it (so return is assigning the argument for print()) or it can assign it to a variable (so return is assigning the value that variable will take on).

def foo(x):
    return x * 2

bar = foo(21)

print (bar)            #  42

print (foo(84 / 4))    #  42

We can, as one sees compose the return value directly in the statement rather than instancing a variable and returning its value. The variable does not get returned, only polled for its value which is sent up the pipeline to the caller.

Maybe so, but that won’t harm anything. Work with what you understand and replace when you are ready to advance in your thinking. My tendency is to compose the return value in place. It’s only a choice, not a better way. Either way is fine.

Just to be clear…

s is a global object. We pass it to the function along with the other two positional arguments expected by the function.

list_without_middle = remove_middle(s, 7, 17)

The variable above is also a global. It takes the value returned by the function. It is not part of the function, proper, only the returned value.

1 Like