strange list behaviour in 2.1.1
Terry Reedy
tjreedy at home.com
Sun Aug 5 12:06:23 EDT 2001
"Adrian Smith" <adrian_p_smith at yahoo.com> wrote in message
news:b9ae8e0f.0108050713.16408c16 at posting.google.com...
> There is a mysterious thing. I wish to take a string from the end of
> one list to the beginning of another, as one might. If I do it
> interactively by hand, with two lists called 'first' and 'second':
>
> >>> second = [first[-1]] + second
You are unbinding 'second' from the original list and rebinding it to
a *new* list generated by the list expression. To do what you
apparently want
second.insert(0,first[-1]) # insert item at front of existing list
Note: time go as len(second)
> >>> del first[-1]
>
> ...it works fine, but if I put it in a procedure, and call it with
the
> parameters 'first' and 'second':
>
> def overflow(firstlist, secondlist):
> secondlist = [firstlist[-1]] + secondlist
Local name 'secondlist' gets rebound, but this has no effect on
outside world
Use insert method to change outside-world list
> del firstlist[-1]
>
> ...the last element of 'first' gets deleted, but 'second' remains
> unaltered. Is there some strange aspect of list assignment I'm
> overlooking here?
No, just normal name-binding rules.
Terry J. Reedy
More information about the Python-list
mailing list