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
http://groups.google.com/group/comp.lang.python/browse_thread/thread/8ab2e67550123b92

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top