# list1.append(list2) returns None

P

#### Pyenos

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?

T

#### Todd Neal

Pyenos said:
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

E

#### Edward Kozlowski

Pyenos said:
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

G

#### Gabriel Genellina

def enlargetable(table,col):

B

#### Ben Finney

Pyenos said:
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>

S

#### Steven D'Aprano

Pyenos said:
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.

P

#### Pyenos

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.

G

#### Georg Brandl

Pyenos said:
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

M

#### Matimus

Pyenos said:
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:
``````def enlargetable(table,col):
table.append(col)
return table``````

G

#### Gabriel Genellina

The following will do exactly the same thing as
the above:

Code:
``````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