A strange behavior of list.extend()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sdhyok

    A strange behavior of list.extend()

    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
  • mackstann

    #2
    Re: A strange behavior of list.extend()

    On Sun, Aug 24, 2003 at 02:11:15PM -0700, sdhyok wrote:[color=blue]
    > 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[/color]

    list.extend happens in place and returns None, so your ls variable is
    just getting the returned None. Same thing happens with sort:
    [color=blue][color=green][color=darkred]
    >>> [4,2,3,1].sort()
    >>>[/color][/color][/color]

    or:
    [color=blue][color=green][color=darkred]
    >>> f = [4,2,3,1]
    >>> f.sort()
    >>> f[/color][/color][/color]
    [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.

    Comment

    • sdhyok

      #3
      Re: A strange behavior of list.extend()

      Thanks. I got it.

      Deahyok

      Comment

      Working...