# list1.append(list2) returns None

Discussion in 'Python' started by Pyenos, Dec 21, 2006.

1. ### PyenosGuest

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?

Pyenos, Dec 21, 2006

2. ### Todd NealGuest

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

Todd

Todd Neal, Dec 21, 2006

3. ### Edward KozlowskiGuest

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?

return the table. Ex:

def enlargetable(table, col):
table.append(col)
return table

Edward Kozlowski, Dec 21, 2006
4. ### Gabriel GenellinaGuest

> > def enlargetable(table,col):
> > return table.append(col)

--
Gabriel Genellina

Gabriel Genellina, Dec 21, 2006
5. ### Ben FinneyGuest

Pyenos <> writes:

> 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?

The answer is both "because that's what it's documented to do":

s.append(x) same as s[len(s):len(s)] = [x]
<URL:http://docs.python.org/lib/typesseq-mutable.html>

and "because you asked it to *do* something, not *get* something":

<URL:http://www.python.org/doc/faq/general.html#why-doesn-t-list-sort-return-the-sorted-list>

--
\ "There was a point to this story, but it has temporarily |
`\ escaped the chronicler's mind." -- Douglas Adams |
_o__) |
Ben Finney

Ben Finney, Dec 21, 2006
6. ### Steven D'ApranoGuest

On Thu, 21 Dec 2006 16:29:01 +1100, Ben Finney wrote:

> Pyenos <> writes:
>
>> 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?

>
> The answer is both "because that's what it's documented to do":

Documentation is a funny thing...

help([].append)

Help on built-in function append:

append(...)
L.append(object) -- append object to end

Sometimes it is hard to tell when you've read enough documentation.
However, as a general rule, "None whatsoever" is rarely enough.

--
Steven

Steven D'Aprano, Dec 21, 2006
7. ### PyenosGuest

i rewrote the code following the advices from subdir of the parent thread:

# well, table is composed of a list of columns.
# so let's stick them together
def enlargetable(table,col):
table.append(col) # the code won't return sorted lists :-o
return_the_fucking_table_bitch=table # assign it to a new variable to return it
return return_the_fucking_table_bitch # :-|

def removecolfromtable(table,col):
return table.remove(col)

print enlargetable([[1],[2],[3]],[4])

# and this code works.

Pyenos, Dec 21, 2006
8. ### Georg BrandlGuest

Pyenos schrieb:
> i rewrote the code following the advices from subdir of the parent thread:
>
> # well, table is composed of a list of columns.
> # so let's stick them together
> def enlargetable(table,col):
> table.append(col) # the code won't return sorted lists :-o

Why should it sort the list?

> return_the_fucking_table_bitch=table # assign it to a new variable to return it
> return return_the_fucking_table_bitch # :-|

That isn't necessary. A simple "return table" is fine.

>
> def removecolfromtable(table,col):
> return table.remove(col)

table.remove() also returns None.

> print enlargetable([[1],[2],[3]],[4])
>
> # and this code works.

Georg

Georg Brandl, Dec 21, 2006
9. ### MatimusGuest

Pyenos wrote:

> def enlargetable(table,col):
> table.append(col) # the code won't return sorted lists :-o
> return_the_fucking_table_bitch=table # assign it to a new variable to return it
> return return_the_fucking_table_bitch # :-|

Maybe you were just trying to be funny, but assiging the table to
another variable before returning is not only unnecessary, it
accomplishes nothing. The following will do exactly the same thing as
the above:

Code (Text):

def enlargetable(table,col):
table.append(col)
return table

Matimus, Dec 21, 2006
10. ### Gabriel GenellinaGuest

At Thursday 21/12/2006 14:50, Matimus wrote:

>The following will do exactly the same thing as
>the above:
>
>
Code (Text):

>def enlargetable(table,col):
>     table.append(col)
>     return table
>

Which, by the way, was one of the first answers he got, by Edward Kozlowski.

--
Gabriel Genellina
Softlab SRL

__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas

Gabriel Genellina, Dec 22, 2006