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

Remember that you can select a slice of a list with lst[index1:index2] You can additionally do maths in this selection such as lst[index1:index2 +1]

My dirty solution? Here it is:

def remove_middle(lst, start, end):
  del(lst[start:end+1])
  return lst

Yeah, not very smart, but it worked.

This assignment, more so than most, shows how many ways there are to complete a task!

Solution using pop()

def remove_middle(lst, start, end):
  for i in range(start,end+1):
    lst.pop(start)
  return lst

I understand codeacademy’s solution. I’m not sure why my answer is not correct. Could you please take a look? Thank you in advance.

def remove_middle(lst, start, end):

new_lst = lst[start:end]
final_lst = lst - new_lst
return final_lst

print(remove_middle([4, 8, 15, 16, 23, 42], 1, 3))

We cannot subtract one list from another. Only concatenate two lists.

1 Like

Have you tried this?
If you do, you’ll see an error message (always read the error messages!!):

TypeError: unsupported operand type(s) for -: ‘list’ and ‘list’

Interpretation: Although the + operator is supported (i.e., defined or “overloaded”) for lists, the - operator is not.

1 Like

Got it - didnt realize that. Thank you!

1 Like