473,578 Members | 3,177 Online

# list1.append(li st2) returns None

def enlargetable(ta ble,col):
return table.append(co l)

def removecolfromta ble(table,col):
return table.remove(co l)

print enlargetable([[1],[2],[3]],[4]) # returns None

Why does it return None instead of [[1],[2],[3],[4]] which I expected?
Dec 21 '06 #1
9 2289
Pyenos wrote:
def enlargetable(ta ble,col):
return table.append(co l)

def removecolfromta ble(table,col):
return table.remove(co l)

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/genera...he-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

Dec 21 '06 #2

Pyenos wrote:
def enlargetable(ta ble,col):
return table.append(co l)

def removecolfromta ble(table,col):
return table.remove(co l)

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(ta ble, col):
table.append(co l)
return table

Dec 21 '06 #3
def enlargetable(ta ble,col):
return table.append(co l)

--
Gabriel Genellina

Dec 21 '06 #4
Pyenos <py****@pyenos. orgwrites:
def enlargetable(ta ble,col):
return table.append(co l)

def removecolfromta ble(table,col):
return table.remove(co l)

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#wh y-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

Dec 21 '06 #5
On Thu, 21 Dec 2006 16:29:01 +1100, Ben Finney wrote:
Pyenos <py****@pyenos. orgwrites:
>def enlargetable(ta ble,col):
return table.append(co l)

def removecolfromta ble(table,col):
return table.remove(co l)

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

Dec 21 '06 #6
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(ta ble,col):
table.append(co l) # the code won't return sorted lists :-o
return_the_fuck ing_table_bitch =table # assign it to a new variable to return it
return return_the_fuck ing_table_bitch # :-|
def removecolfromta ble(table,col):
return table.remove(co l)

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

# and this code works.
Dec 21 '06 #7
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(ta ble,col):
table.append(co l) # the code won't return sorted lists :-o
Why should it sort the list?
return_the_fuck ing_table_bitch =table # assign it to a new variable to return it
return return_the_fuck ing_table_bitch # :-|
That isn't necessary. A simple "return table" is fine.
>
def removecolfromta ble(table,col):
return table.remove(co l)
table.remove() also returns None.
print enlargetable([[1],[2],[3]],[4])

# and this code works.
Georg
Dec 21 '06 #8

Pyenos wrote:
def enlargetable(ta ble,col):
table.append(co l) # the code won't return sorted lists :-o
return_the_fuck ing_table_bitch =table # assign it to a new variable to return it
return return_the_fuck ing_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:

Expand|Select|Wrap|Line Numbers
1. def enlargetable(table,col):
2. table.append(col)
3. return table
4.
Dec 21 '06 #9
At Thursday 21/12/2006 14:50, Matimus wrote:
>The following will do exactly the same thing as
the above:

Expand|Select|Wrap|Line Numbers
1. def enlargetable(table,col):
2.     table.append(col)
3.     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

Dec 22 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.