<br><div class="gmail_quote"><div class="gmail_quote"><div class="im"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
The list nlist inside of function xx is not the same as the variable u outside of the function:  nlist and u refer to two separate list objects.  When you modify nlist, you are not modifying u. <span><font color="#888888"><a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank"></a></font></span></blockquote>

</div><div><br>Well - that's not quite true. Before calling the function, u is [1, 2, 3, 4] - but after calling the function,  u is [1, 2, 3, 4, 999]. This is a result of using 'nlist += [999]' - the same thing doesn't happen if you use 'nlist = nlist+[999]' instead.<br>

<br>I'm not completely aware of what's going on behind the scenes here, but I think the problem is that 'nlist' is actually a reference to a list object - it points to the same place as u. When you assign to it within the function, then it becomes separate from u - which is why nlist = nlist+[999] and nlist = nlist[:-1] don't modify u - but if you modify nlist in place before doing that, such as by using +=, then it's still pointing to u, and so u gets modified as well.<br>

</div></div><br>So, for example:<br><br>>>> def blank_list(nlist):<br>...     nlist[:] = [] # modifies the list in-place<br>... <br>>>> u<br>[1, 2, 3, 4]<br>>>> blank_list(u)<br>>>> u<br>

[] # u has been changed by our change to nlist!<br><br>But if we change it so that it assigns to nlist rather than modifying it:<br><br>>>> def dont_blank_list(nlist):<br>...     nlist = [] # creates a new list object rather than modifying the current one<br>

... <br>>>> u = [1, 2, 3, 4]<br>>>> u<br>[1, 2, 3, 4]<br>>>> dont_blank_list(u)<br>>>> u<br>[1, 2, 3, 4] # u is completely unaffected!<span class="HOEnZb"><font color="#888888"><br>-- <br>
Robert K. Day<div><a href="mailto:robert.day@merton.oxon.org" target="_blank">robert.day@merton.oxon.org</a></div>
<br>
</font></span></div><br><br clear="all"><br>-- <br>Robert K. Day<div><a href="mailto:robert.day@merton.oxon.org" target="_blank">robert.day@merton.oxon.org</a></div><br>