Puzzled by list-appending behavior

Chris Angelico rosuav at gmail.com
Thu May 26 03:20:53 EDT 2011


On Thu, May 26, 2011 at 2:46 PM, Uncle Ben <bgreen at nycap.rr.com> 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]

Ben Finney has already answered the main question, but as a side
point, I would generally avoid creating a variable called 'list'.
That's the name of the type (Python 2) or class (Python 3) of all
lists, so it might result in confusion if you have an actual list with
that name.

If you want the behaviour of joining two lists (or adding something to
a list) and saving the result elsewhere, you can use the plain
addition:

a=[1,2,3]
b=a+[[4,5,6]]

Note that you have to add a list, and it will append the contents of that list.

Chris Angelico



More information about the Python-list mailing list