list.extend([]) Question

Discussion in 'Python' started by Dan Brown, Jan 30, 2010.

  1. Dan Brown

    Dan Brown Guest

    Why does extending a list with the empty list result in None? It
    seems very counterintuitive to me, at least --- I expected ['a'].extend
    ([]) to result in ['a'], not None.
     
    Dan Brown, Jan 30, 2010
    #1
    1. Advertising

  2. * Dan Brown:
    > Why does extending a list with the empty list result in None? It
    > seems very counterintuitive to me, at least --- I expected ['a'].extend
    > ([]) to result in ['a'], not None.


    It does.

    'extend' is an operation that /modifies/ the array.

    It just returns None as its expression result, in the same way as e.g. the
    Python 3.x 'print' (another pure "doer" operation).

    >>> L = ['a']
    >>> L

    ['a']
    >>> L2 = L.extend( [] )
    >>> L2
    >>> L2 is None

    True
    >>> L

    ['a']
    >>> _



    Cheers & hth.,

    - Alf
     
    Alf P. Steinbach, Jan 30, 2010
    #2
    1. Advertising

  3. Dan Brown

    Andre Engels Guest

    On Sat, Jan 30, 2010 at 4:32 PM, Dan Brown <> wrote:
    > Why does extending a list with the empty list result in None?  It
    > seems very counterintuitive to me, at least --- I expected ['a'].extend
    > ([]) to result in ['a'], not None.


    Extend is a method of the list. The list itself is changed, it does
    not return itself:

    >>> A = [1,2]
    >>> B = [3,4]
    >>> C = A.extend(B)
    >>> C
    >>> C is None

    True
    >>> A

    [1, 2, 3, 4]


    Thus, nothing special about extend([]), this is the general behaviour of extend

    --
    André Engels,
     
    Andre Engels, Jan 30, 2010
    #3
  4. Dan Brown

    Dan Brown Guest

    On Jan 30, 8:38 am, "Alf P. Steinbach" <> wrote:
    > It does.
    >
    > 'extend' is an operation that /modifies/ the array.
    >
    > It just returns None as its expression result, in the same way as e.g. the
    > Python 3.x 'print' (another pure "doer" operation).
    >
    >    >>> L = ['a']
    >    >>> L
    >    ['a']
    >    >>> L2 = L.extend( [] )
    >    >>> L2
    >    >>> L2 is None
    >    True
    >    >>> L
    >    ['a']
    >    >>> _
    >
    > Cheers & hth.,
    >
    > - Alf


    Aha. Well, I feel a bit silly for not thinking to try it that way.
    Thanks!
     
    Dan Brown, Jan 30, 2010
    #4
  5. Dan Brown

    Steve Holden Guest

    Dan Brown wrote:
    > Why does extending a list with the empty list result in None? It
    > seems very counterintuitive to me, at least --- I expected ['a'].extend
    > ([]) to result in ['a'], not None.


    How very inconvenient of Python! What it actually does is create an
    anonymous list containing only the element 'a', and leave it unchanged
    by extending it with an empty list. Since there is no longer any
    reference to the list it has become garbage.

    Contrast that with:

    >>> lst = ['a']
    >>> lst.extend([])
    >>> lst

    ['a']
    >>> lst.append([])
    >>> lst

    ['a', []]
    >>> lst.extend(['1'])
    >>> lst

    ['a', [], '1']
    >>>


    As you can see by the absence of output, both the .extend() and
    ..append() list methods return None. They mutate the list instance upon
    which they are called.

    In your example you were expecting the methods to return the mutated
    list. They don't.

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    PyCon is coming! Atlanta, Feb 2010 http://us.pycon.org/
    Holden Web LLC http://www.holdenweb.com/
    UPCOMING EVENTS: http://holdenweb.eventbrite.com/
     
    Steve Holden, Jan 30, 2010
    #5
  6. Dan Brown

    Aahz Guest

    Aahz, Feb 5, 2010
    #6
  7. I think it's because when you do ['a'].extend([]) or whatever, the
    result is whatever the method "extend" returns. But "extend" has no
    return value, hence you will see None if you do this interactively.

    On Fri, Feb 5, 2010 at 10:55 AM, Aahz <> wrote:
    > In article <>,
    > Dan Brown  <> wrote:
    >>
    >>Why does extending a list with the empty list result in None?  It
    >>seems very counterintuitive to me, at least --- I expected ['a'].extend
    >>([]) to result in ['a'], not None.

    >
    > http://www.python.org/doc/faq/general/#why-doesn-t-list-sort-return-the-sorted-list
    > --
    > Aahz ()           <*>         http://www.pythoncraft.com/
    >
    > import antigravity
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >




    --
    Gerald Britton
     
    Gerald Britton, Feb 5, 2010
    #7
  8. Gerald Britton a écrit :
    > I think it's because when you do ['a'].extend([]) or whatever, the
    > result is whatever the method "extend" returns. But "extend" has no
    > return value


    It does : it returns None.
     
    Bruno Desthuilliers, Feb 5, 2010
    #8
  9. On Fri, Feb 5, 2010 at 11:31 AM, Gerald Britton
    <> wrote:
    > I think it's because when you do ['a'].extend([]) or whatever, the
    > result is whatever the method "extend" returns.  But "extend" has no
    > return value, hence you will see None if you do this interactively.
    >


    That sums it up. In Python the convention is to raise an exception on
    error, return a new value in the case where a new value is created,
    and - as in this case - to return None for modification of an existing
    value. Returning "None" is vexing if you are used to another language
    that has a different convention but is expected for well behaved
    python libraries.

    So yeah, Python does it that way because the intent is to loudly and
    regularly announce that something was modified or to loudly and
    regularly announce that something new was /not/ created. It's a
    boring story with no whiz-bang feature behind it, but I like that the
    language behaves that way for exactly that reason.

    -Jack
     
    Jack Diederich, Feb 5, 2010
    #9
    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. sdhyok
    Replies:
    2
    Views:
    302
    sdhyok
    Aug 25, 2003
  2. James Stroud

    extension to list extend

    James Stroud, Oct 16, 2007, in forum: Python
    Replies:
    2
    Views:
    294
    James Stroud
    Oct 16, 2007
  3. Chris Rebert

    Re: faster than list.extend()

    Chris Rebert, Nov 16, 2009, in forum: Python
    Replies:
    2
    Views:
    305
    Jason Sewall
    Nov 17, 2009
  4. Gabriel Genellina

    Re: faster than list.extend()

    Gabriel Genellina, Nov 17, 2009, in forum: Python
    Replies:
    1
    Views:
    342
    Peter Otten
    Nov 17, 2009
  5. Sebastian Padó

    extend question

    Sebastian Padó, Sep 14, 2005, in forum: Ruby
    Replies:
    4
    Views:
    102
    Caleb Clausen
    Sep 14, 2005
Loading...

Share This Page