A strange behavior of list.extend()

Discussion in 'Python' started by sdhyok, Aug 24, 2003.

  1. sdhyok

    sdhyok Guest

    I think the two script should produce the same results, but not.
    Why?

    ---------------- Script #1
    ls = [1]
    ls.extend([2,3])
    print ls
    -> [1,2,3]

    --------------- Script #2
    ls = [1].extend([2,3])
    print ls
    -> None

    Daehyok
     
    sdhyok, Aug 24, 2003
    #1
    1. Advertising

  2. sdhyok

    mackstann Guest

    On Sun, Aug 24, 2003 at 02:11:15PM -0700, sdhyok wrote:
    > I think the two script should produce the same results, but not.
    > Why?
    >
    > ---------------- Script #1
    > ls = [1]
    > ls.extend([2,3])
    > print ls
    > -> [1,2,3]
    >
    > --------------- Script #2
    > ls = [1].extend([2,3])
    > print ls
    > -> None


    list.extend happens in place and returns None, so your ls variable is
    just getting the returned None. Same thing happens with sort:

    >>> [4,2,3,1].sort()
    >>>


    or:

    >>> f = [4,2,3,1]
    >>> f.sort()
    >>> f

    [1, 2, 3, 4]


    --
    m a c k s t a n n mack @ incise.org http://incise.org
    Excellent time to become a missing person.
     
    mackstann, Aug 24, 2003
    #2
    1. Advertising

  3. sdhyok

    sdhyok Guest

    Thanks. I got it.

    Deahyok
     
    sdhyok, Aug 25, 2003
    #3
    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. George Henry

    Strange (?) list comprehension behavior

    George Henry, Jul 19, 2003, in forum: Python
    Replies:
    3
    Views:
    435
    Peter Hansen
    Jul 20, 2003
  2. Replies:
    7
    Views:
    259
    Arnaud Delobelle
    Nov 25, 2008
  3. Steven Daedelus

    extend behavior to congruent div

    Steven Daedelus, May 27, 2004, in forum: Javascript
    Replies:
    5
    Views:
    107
    Steven Daedelus
    May 28, 2004
  4. Robrecht W. Uyttenhove

    Strange behavior for a 2D list

    Robrecht W. Uyttenhove, Apr 18, 2013, in forum: Python
    Replies:
    1
    Views:
    138
    John Gordon
    Apr 18, 2013
  5. Peter Otten

    Re: Strange behavior for a 2D list

    Peter Otten, Apr 18, 2013, in forum: Python
    Replies:
    0
    Views:
    128
    Peter Otten
    Apr 18, 2013
Loading...

Share This Page