Puzzled by list-appending behavior

Algis Kabaila akabaila at pcug.org.au
Thu May 26 03:18:36 EDT 2011


On Thursday 26 May 2011 14:46:45 Uncle Ben wrote:
> In playing with lists of lists, I found the following:
> 
> (In 3.1, but the same happens also in 2.7)
> 
> list = [1,2,3]
> list.append ( [4,5,6] )
> x = list
> x   ->
>     [1,2,3,[4,5,6]]
> as expected.
> 
> But the shortcut fails:
> 
> list=[1,2,3]
> x = list.append( [4,5,6] )
> x   ->
>    nothing
> 
> Can someone explain this to me?
> 
> Uncle Ben

And why do you insist on calling and instance of list, "list"? 
Even a human reader will confuse which is which.  What you are 
showing is an example how confusing things become when a keyword 
(list) is over-written (with  list instance).

Why don't you just say

lst = [1, 2, 3]

rather than list =...   It may be permissible, but it is not a 
good idea!

Old (practical) Al.
-- 
Algis
http://akabaila.pcug.org.au/StructuralAnalysis.pdf
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110526/510b5d3b/attachment.html>


More information about the Python-list mailing list