I have been learning to program in python, and came across this question which I have been struggling to solve. The question is as follows:
Write a function f(list, start, end) which takes as arguments a list and two indices and modifies the argument list so that it is equal to the result of the slice expression list[start:end]
I can write a function that splices the list for positive indices, ie:
def f(this_list, start, end):
this_list=this_list[start:end+1]
But how do I get it to update whatever list the function is pointed to in the global namespace?
So, for instance, if I then get it to run:
x=[1, 2, 3, 4, 5]
f(x, 2, 4)
print x
it returns the originally defined x, not the updated. So this is because it has only updated the list in the function's namespace, yes? But then how can I get it to update x globally?