append on lists

Jerry Hill malaclypse2 at gmail.com
Mon Sep 15 22:50:18 CEST 2008

```On Mon, Sep 15, 2008 at 4:45 PM, Armin <a at nospam.org> wrote:
> Yes, but this is very unconvenient.
> If d should reference the list a extended with a single list element
> you need at least two lines

You do it in two lines, because you're doing two different things.

> a.append(7)

This appends the element 7 to the list a.

> d=a

This binds the name d to the same list as a is bound to.  If you wand
d to point to a new list with the same contents as the list a, plus
the number 7 do this:

d = a + [7]

Here's an example of the difference:
>>> a = range(6)
>>> a
[0, 1, 2, 3, 4, 5]
>>> a.append(7)
>>> a
[0, 1, 2, 3, 4, 5, 7]
>>> d = a
>>> d
[0, 1, 2, 3, 4, 5, 7]
>>> d.append(10)
>>> a
[0, 1, 2, 3, 4, 5, 7, 10]
>>> d
[0, 1, 2, 3, 4, 5, 7, 10]
>>>

See how a and d are two names bound to the same list?

Here's the other way:
>>> a = range(6)
>>> d = a + [7]
>>> a
[0, 1, 2, 3, 4, 5]
>>> d
[0, 1, 2, 3, 4, 5, 7]
>>> d.append(10)
>>> a
[0, 1, 2, 3, 4, 5]
>>> d
[0, 1, 2, 3, 4, 5, 7, 10]
>>>

--
Jerry

```