append on lists

Chris Rebert cvrebert at gmail.com
Mon Sep 15 22:21:31 CEST 2008


On Mon, Sep 15, 2008 at 1:24 PM, Armin <a at nospam.org> wrote:
>
>
> Hi,
>
> just a dumb question.
>
> Let a = [1,2,3,4,5]
>
> Why is the value of a.append(7) equal None and not [1,2,3,4,5,6,7] ??
I'll assume the presence of the 6 is a typo.

Because .append() mutates 'a' and appends the item in-place rather
than creating and returning a new list with the item appended, and
it's good Python style for mutating methods to have no return value
(since all functions must have some return value, Python uses None
when the function doesn't explicitly return anything).

If you print 'a' after doing the .append(), you'll see it's changed to
your desired value.

Regards,
Chris

>
> --Armin
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
Follow the path of the Iguana...
http://rebertia.com



More information about the Python-list mailing list