list1.append(list2) returns None

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

  1. Pyenos

    Pyenos Guest

    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
    #1
    1. Advertising

  2. Pyenos

    Todd Neal Guest

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

    Todd
    Todd Neal, Dec 21, 2006
    #2
    1. Advertising

  3. 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
    #3
  4. > > def enlargetable(table,col):
    > > return table.append(col)


    Google for "python pitfalls"

    --
    Gabriel Genellina
    Gabriel Genellina, Dec 21, 2006
    #4
  5. Pyenos

    Ben Finney Guest

    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
    #5
  6. 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
    #6
  7. Pyenos

    Pyenos Guest

    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
    #7
  8. Pyenos

    Georg Brandl Guest

    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
    #8
  9. Pyenos

    Matimus Guest

    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:
    def enlargetable(table,col):
        table.append(col)
        return table
    
    Matimus, Dec 21, 2006
    #9
  10. 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
    Gabriel Genellina, Dec 22, 2006
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Klaus Neuner
    Replies:
    4
    Views:
    286
    Klaus Neuner
    Feb 10, 2004
  2. length power
    Replies:
    2
    Views:
    65
    Rustom Mody
    Apr 10, 2014
  3. Skip Montanaro
    Replies:
    0
    Views:
    48
    Skip Montanaro
    Apr 10, 2014
  4. Johannes Schneider

    Re: why i have the output of [None, None, None]

    Johannes Schneider, Apr 10, 2014, in forum: Python
    Replies:
    0
    Views:
    43
    Johannes Schneider
    Apr 10, 2014
  5. Terry Reedy
    Replies:
    0
    Views:
    53
    Terry Reedy
    Apr 10, 2014
Loading...

Share This Page