Appending to []

Chris Angelico rosuav at gmail.com
Fri Apr 20 16:09:18 EDT 2012


On Sat, Apr 21, 2012 at 6:03 AM, Jan Sipke <jansipke at gmail.com> wrote:
> Can you explain why there is a difference between the following two
> statements?
>
>>>> a = []
>>>> a.append(1)
>>>> print a
> [1]

This looks at the list after appending.

>>>> print [].append(1)
> None

This looks at the return value of the append() method. Since it
doesn't return self, the return value isn't much use. Incidentally,
your function call in the first block would have displayed the return
value had it not been None - that's how the interactive interpreter
works.

Chris Angelico



More information about the Python-list mailing list