Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > list1.append(list2) returns None

Reply
Thread Tools

list1.append(list2) returns None

 
 
Pyenos
Guest
Posts: n/a
 
      12-21-2006
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?
 
Reply With Quote
 
 
 
 
Todd Neal
Guest
Posts: n/a
 
      12-21-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/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
http://groups.google.com/group/comp....b2e67550123b92

Todd

 
Reply With Quote
 
 
 
 
Edward Kozlowski
Guest
Posts: n/a
 
      12-21-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?


return the table. Ex:

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

 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      12-21-2006
> > def enlargetable(table,col):
> > return table.append(col)


Google for "python pitfalls"

--
Gabriel Genellina

 
Reply With Quote
 
Ben Finney
Guest
Posts: n/a
 
      12-21-2006
Pyenos <(E-Mail Removed)> 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

 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      12-21-2006
On Thu, 21 Dec 2006 16:29:01 +1100, Ben Finney wrote:

> Pyenos <(E-Mail Removed)> 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

 
Reply With Quote
 
Pyenos
Guest
Posts: n/a
 
      12-21-2006
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
return_the_****ing_table_bitch=table # assign it to a new variable to return it
return return_the_****ing_table_bitch #


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

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

# and this code works.
 
Reply With Quote
 
Georg Brandl
Guest
Posts: n/a
 
      12-21-2006
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


Why should it sort the list?

> return_the_****ing_table_bitch=table # assign it to a new variable to return it
> return return_the_****ing_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
 
Reply With Quote
 
Matimus
Guest
Posts: n/a
 
      12-21-2006

Pyenos wrote:

> def enlargetable(table,col):
> table.append(col) # the code won't return sorted lists
> return_the_****ing_table_bitch=table # assign it to a new variable to return it
> return return_the_****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:

Code:
def enlargetable(table,col):
    table.append(col)
    return table
 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      12-22-2006
At Thursday 21/12/2006 14:50, Matimus wrote:

>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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DataGrid - BorderStyle="None" and BorderWidth="None" doesn't work for Firefox David Freeman ASP .Net 8 02-16-2011 11:03 AM
Re: <var> is None vs. <var> == None Steve Holden Python 9 01-26-2009 09:10 PM
<var> is None vs. <var> == None Gerald Britton Python 7 01-24-2009 09:02 AM
testing for valid reference: obj vs. None!=obs vs. obj is not None alf Python 9 12-09-2006 05:00 AM
Is there a built-in method for transforming (1,None,"Hello!") to 1,None,"Hello!"? Daniel Crespo Python 5 11-13-2005 12:52 PM



Advertisments