Changing a global variable in a function without using global keyword
<p><a href="http://i.stack.imgur.com/EQUph.jpg" rel="nofollow"><img src="http://i.stack.imgur.com/EQUph.jpg" alt="screenshot of code from a textbook"></a></p>

<p>Here is my code:</p>

<pre><code>def L_value_Change(k):
    global L
    L = k
    return L

def  applyF_filterG(L, f, g):
    L
    k = []
    for i in L:
       if g(f(i)):
          k.append(i)
    L = k
    L_value_Change(k)
    if L == []:
        return -1
    else :
        return max(L)
</code></pre>

<p>When I put this code, the grader tells me it's incorrect! So I read the introduction to the quiz, the instructor wrote "global variables don't work". How could I change the <code>L</code> variable with a function without using the word <code>global</code>? If you try my code and gave it the required input, it will give you a right answer, but the grader tells me it's wrong.</p>

<p>You need the <code>global</code> keyword when you want to rebind the global variable to a different object. But you don't need it if all you want to do is change a mutable object. In your case <code>L</code> is a list and can be mutated in place with a slice operation <code>L[:] = k</code>. To demonstrate:</p>

<pre><code>&gt;&gt;&gt; L = [1,2,3]
&gt;&gt;&gt; 
&gt;&gt;&gt; def L_value_Change(k):
...     L[:] = k
... 
&gt;&gt;&gt; old_id = id(L)
&gt;&gt;&gt; L
[1, 2, 3]
&gt;&gt;&gt; L_value_Change([4,5,6])
&gt;&gt;&gt; assert id(L) == old_id
&gt;&gt;&gt; L
[4, 5, 6]
&gt;&gt;&gt; 
</code></pre>

python python-3.x