Python, How to Define a Function whose input is a Variable in the Global Namespace it must then Alter
<p>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:</p>

<p>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]</p>

<p>I can write a function that splices the list for positive indices, ie:</p>

<pre><code>def f(this_list, start, end):
    this_list=this_list[start:end+1]
</code></pre>

<p>But how do I get it to update whatever list the function is pointed to in the global namespace?</p>

<p>So, for instance, if I then get it to run:</p>

<pre><code>x=[1, 2, 3, 4, 5]
f(x, 2, 4)
print x
</code></pre>

<p>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?</p>

<p>As someone already mentioned in the comments, you should definitely read <a href="http://nedbatchelder.com/text/names.html" rel="nofollow">Facts and myths about Python names and values</a></p>

<p>The simple solution to your problem is making a new value and returning it.
This will work : </p>

<pre><code>def f(this_list, start, end):
    this_list=this_list[start:end+1]
    return this_list
</code></pre>

python list function