list1.append(list2) returns None

Todd Neal tolchz at gmail.com
Wed Dec 20 22:48:51 EST 2006


Pyenos wrote:
> def enlargetable(table,col):
>     return table.append(col)
>
> def removecolfromtable(table,col):
>     return table.remove(col)
>
> print enlargetable([[1],[2],[3]],[4]) # returns None
>
> Why does it return None instead of [[1],[2],[3],[4]] which I expected?

append modifies the list and then returns None:

>>> print a
[1, 2, 3]
>>> print a.append(4)
None
>>> print a
[1, 2, 3, 4]


The reasoning given at
http://www.python.org/doc/faq/general.html#why-doesn-t-list-sort-return-the-sorted-list
is so you wont do something like this:

a = [1,2,3]
b = a.append(4)

and assume that a is still [1,2,3]


More discussion on this topic is available at
http://groups.google.com/group/comp.lang.python/browse_thread/thread/8ab2e67550123b92

Todd




More information about the Python-list mailing list