list.clear() missing?!?

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

    #1

    list.clear() missing?!?

    I tried to clear a list today (which I do rather rarely, considering
    that just doing l = [] works most of the time) and was shocked, SHOCKED
    to notice that there is no clear() method. Dicts have it, sets have it,
    why do lists have to be second class citizens?

  • Fredrik Lundh

    #2
    Re: list.clear() missing?!?

    Ville Vainio wrote:
    [color=blue]
    > I tried to clear a list today (which I do rather rarely, considering
    > that just doing l = [] works most of the time) and was shocked, SHOCKED
    > to notice that there is no clear() method. Dicts have it, sets have it,
    > why do lists have to be second class citizens?[/color]

    because Python already has a perfectly valid way to clear a list,
    perhaps ?

    del l[:]

    (lists are not mappings, so the duck typing argument don't really
    apply here.)

    </F>



    Comment

    • Ville Vainio

      #3
      Re: list.clear() missing?!?

      Fredrik Lundh wrote:
      [color=blue][color=green]
      > > I tried to clear a list today (which I do rather rarely, considering
      > > that just doing l = [] works most of the time) and was shocked, SHOCKED
      > > to notice that there is no clear() method. Dicts have it, sets have it,
      > > why do lists have to be second class citizens?[/color]
      >
      > because Python already has a perfectly valid way to clear a list,
      > perhaps ?
      >
      > del l[:][/color]

      Ok. That's pretty non-obvious but now that I've seen it I'll probably
      remember it. I did a stupid "while l: l.pop()" loop myself.
      [color=blue]
      > (lists are not mappings, so the duck typing argument don't really
      > apply here.)[/color]

      I was thinking of list as a "mutable collection", and clear() is
      certainly a very natural operation for them.

      Comment

      • Steven Bethard

        #4
        Re: list.clear() missing?!?

        Ville Vainio wrote:[color=blue]
        > I tried to clear a list today (which I do rather rarely, considering
        > that just doing l = [] works most of the time) and was shocked, SHOCKED
        > to notice that there is no clear() method. Dicts have it, sets have it,
        > why do lists have to be second class citizens?[/color]

        This gets brought up all the time (search the archives for your
        favorite), but your options are basically (renaming your list to lst for
        readability) one of::

        del lst[:]

        lst[:] = []

        or if you don't need to modify the list in place,

        lst = []

        Personally, I tend to go Fredrik's route and use the first.

        If you feel really strongly about this though, you might consider
        writing up a PEP. It's been contentious enough that there's not much
        chance of getting a change without one.

        STeVe

        Comment

        • Felipe Almeida Lessa

          #5
          Re: list.clear() missing?!?

          Em Ter, 2006-04-11 às 10:42 -0600, Steven Bethard escreveu:[color=blue]
          > one of::
          >
          > del lst[:]
          >
          > lst[:] = []
          >
          > or if you don't need to modify the list in place,
          >
          > lst = []
          >
          > Personally, I tend to go Fredrik's route and use the first.[/color]

          I love benchmarks, so as I was testing the options, I saw something very
          strange:

          $ python2.4 -mtimeit 'x = range(100000); '
          100 loops, best of 3: 6.7 msec per loop
          $ python2.4 -mtimeit 'x = range(100000); del x[:]'
          100 loops, best of 3: 6.35 msec per loop
          $ python2.4 -mtimeit 'x = range(100000); x[:] = []'
          100 loops, best of 3: 6.36 msec per loop
          $ python2.4 -mtimeit 'x = range(100000); del x'
          100 loops, best of 3: 6.46 msec per loop

          Why the first benchmark is the slowest? I don't get it... could someone
          test this, too?

          Cheers,

          --
          Felipe.

          Comment

          • Ville Vainio

            #6
            Re: list.clear() missing?!?

            Steven Bethard wrote:
            [color=blue]
            > If you feel really strongly about this though, you might consider
            > writing up a PEP. It's been contentious enough that there's not much
            > chance of getting a change without one.[/color]

            No strong feelings here, and I'm sure greater minds than me have
            already hashed this over sufficiently.

            It's just that, when I have an object, and am wondering how I can clear
            it, I tend to look what methods it has first and go to google looking
            for "idioms" second.

            Perhaps "clear" method could be added that raises
            PedagogicExcept ion("Use del lst[:], stupid!")?

            *ducks*

            Comment

            • Martin v. Löwis

              #7
              Re: list.clear() missing?!?

              Felipe Almeida Lessa wrote:[color=blue]
              > I love benchmarks, so as I was testing the options, I saw something very
              > strange:
              >
              > $ python2.4 -mtimeit 'x = range(100000); '
              > 100 loops, best of 3: 6.7 msec per loop
              > $ python2.4 -mtimeit 'x = range(100000); del x[:]'
              > 100 loops, best of 3: 6.35 msec per loop
              > $ python2.4 -mtimeit 'x = range(100000); x[:] = []'
              > 100 loops, best of 3: 6.36 msec per loop
              > $ python2.4 -mtimeit 'x = range(100000); del x'
              > 100 loops, best of 3: 6.46 msec per loop
              >
              > Why the first benchmark is the slowest? I don't get it... could someone
              > test this, too?[/color]

              In the first benchmark, you need space for two lists: the old one and
              the new one; the other benchmarks you need only a single block of
              memory (*). Concluding from here gets difficult - you would have to study
              the malloc implementation to find out whether it works better in one
              case over the other. Could also be an issue of processor cache: one
              may fit into the cache, but the other may not.

              Regards,
              Martin

              (*) plus, you also need the integer objects twice.

              Comment

              • Martin v. Löwis

                #8
                Re: list.clear() missing?!?

                Ville Vainio wrote:[color=blue]
                > It's just that, when I have an object, and am wondering how I can clear
                > it, I tend to look what methods it has first and go to google looking
                > for "idioms" second.[/color]

                I guess del on a list is not that common, so people tend to not know
                that it works on lists (and slices!), too. It's too bad that lists have
                a pop() method these days, so people can do x.pop() even if they don't
                need the value, instead of doing del x[-1]. I don't think I ever needed
                to del a slice except for clearing the entire list (and I don't need to
                do that often, either - I just throw the list away).

                Regards,
                Martin

                Comment

                • John Salerno

                  #9
                  Re: list.clear() missing?!?

                  Steven Bethard wrote:

                  [color=blue]
                  > lst[:] = []
                  > lst = [][/color]

                  What's the difference here?

                  Comment

                  • Felipe Almeida Lessa

                    #10
                    Re: list.clear() missing?!?

                    Em Ter, 2006-04-11 às 17:56 +0000, John Salerno escreveu:[color=blue]
                    > Steven Bethard wrote:
                    >
                    >[color=green]
                    > > lst[:] = []
                    > > lst = [][/color]
                    >
                    > What's the difference here?[/color]

                    lst[:] = [] makes the specified slice become []. As we specified ":", it
                    transforms the entire list into [].

                    lst = [] assigns the value [] to the variable lst, deleting any previous
                    one.

                    This might help:
                    [color=blue][color=green][color=darkred]
                    >>> lst = range(10)
                    >>> id(lst), lst[/color][/color][/color]
                    (-1210826356, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])[color=blue][color=green][color=darkred]
                    >>> lst[:] = []
                    >>> id(lst), lst[/color][/color][/color]
                    (-1210826356, [])
                    [color=blue][color=green][color=darkred]
                    >>> lst = range(10)
                    >>> id(lst), lst[/color][/color][/color]
                    (-1210844052, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])[color=blue][color=green][color=darkred]
                    >>> lst = []
                    >>> id(lst), lst[/color][/color][/color]
                    (-1210826420, [])


                    You see? lst[:] removes all elements from the list that lst refers to,
                    while lst = [] just creates a new list and discard the only one. The
                    difference is, for example:
                    [color=blue][color=green][color=darkred]
                    >>> lst = range(3)
                    >>> x = [lst, lst, lst]
                    >>> x[/color][/color][/color]
                    [[0, 1, 2], [0, 1, 2], [0, 1, 2]][color=blue][color=green][color=darkred]
                    >>> lst[:] = []
                    >>> x[/color][/color][/color]
                    [[], [], []]
                    [color=blue][color=green][color=darkred]
                    >>> lst = range(3)
                    >>> x = [lst, lst, lst]
                    >>> x[/color][/color][/color]
                    [[0, 1, 2], [0, 1, 2], [0, 1, 2]][color=blue][color=green][color=darkred]
                    >>> lst = []
                    >>> x[/color][/color][/color]
                    [[0, 1, 2], [0, 1, 2], [0, 1, 2]]

                    HTH,

                    --
                    Felipe.

                    Comment

                    • Fredrik Lundh

                      #11
                      Re: list.clear() missing?!?

                      John Salerno wrote:
                      [color=blue]
                      > Steven Bethard wrote:
                      >
                      >[color=green]
                      > > lst[:] = []
                      > > lst = [][/color]
                      >
                      > What's the difference here?[/color]

                      L[:]= modifies the object in place, L=[] binds the variable to a
                      new object. compare and contrast:
                      [color=blue][color=green][color=darkred]
                      >>> L = ["a", "b", "c"]
                      >>> M = L
                      >>> L[/color][/color][/color]
                      ['a', 'b', 'c'][color=blue][color=green][color=darkred]
                      >>> M[/color][/color][/color]
                      ['a', 'b', 'c'][color=blue][color=green][color=darkred]
                      >>> L is M[/color][/color][/color]
                      True[color=blue][color=green][color=darkred]
                      >>> L[:] = []
                      >>> L[/color][/color][/color]
                      [][color=blue][color=green][color=darkred]
                      >>> M[/color][/color][/color]
                      [][color=blue][color=green][color=darkred]
                      >>> L is M[/color][/color][/color]
                      True
                      [color=blue][color=green][color=darkred]
                      >>> L = ["a", "b", "c"]
                      >>> M = L
                      >>> L[/color][/color][/color]
                      ['a', 'b', 'c'][color=blue][color=green][color=darkred]
                      >>> M[/color][/color][/color]
                      ['a', 'b', 'c'][color=blue][color=green][color=darkred]
                      >>> L = []
                      >>> L[/color][/color][/color]
                      [][color=blue][color=green][color=darkred]
                      >>> M[/color][/color][/color]
                      ['a', 'b', 'c'][color=blue][color=green][color=darkred]
                      >>> L is M[/color][/color][/color]
                      False

                      </F>



                      Comment

                      • Duncan Smith

                        #12
                        Re: list.clear() missing?!?

                        John Salerno wrote:[color=blue]
                        > Steven Bethard wrote:
                        >
                        >[color=green]
                        >> lst[:] = []
                        >> lst = [][/color]
                        >
                        >
                        > What's the difference here?[/color]
                        [color=blue][color=green][color=darkred]
                        >>> lst = [1,2,3]
                        >>> lst2 = lst
                        >>> lst[:] = []
                        >>> lst2[/color][/color][/color]
                        [][color=blue][color=green][color=darkred]
                        >>> lst = [1,2,3]
                        >>> lst2 = lst
                        >>> lst = []
                        >>> lst2[/color][/color][/color]
                        [1, 2, 3][color=blue][color=green][color=darkred]
                        >>>[/color][/color][/color]

                        Duncan

                        Comment

                        • John Salerno

                          #13
                          Re: list.clear() missing?!?

                          Felipe Almeida Lessa wrote:
                          [color=blue]
                          > You see? lst[:] removes all elements from the list that lst refers to,
                          > while lst = [] just creates a new list and discard the only one. The
                          > difference is, for example:[/color]

                          Thanks, your explanation was great!

                          Comment

                          • John Salerno

                            #14
                            Re: list.clear() missing?!?

                            Fredrik Lundh wrote:[color=blue]
                            > John Salerno wrote:
                            >[color=green]
                            >> Steven Bethard wrote:
                            >>
                            >>[color=darkred]
                            >>> lst[:] = []
                            >>> lst = [][/color]
                            >> What's the difference here?[/color]
                            >
                            > L[:]= modifies the object in place, L=[] binds the variable to a
                            > new object. compare and contrast:[/color]

                            Thanks guys, your explanations are really helpful. I think what had me
                            confused at first was my understanding of what L[:] does on either side
                            of the assignment operator. On the left, it just chooses those elements
                            and edits them in place; on the right, it makes a copy of that list,
                            right? (Which I guess is still more or less *doing* the same thing, just
                            for different purposes)

                            Comment

                            • Ville Vainio

                              #15
                              Re: list.clear() missing?!?

                              John Salerno wrote:
                              [color=blue]
                              > Thanks guys, your explanations are really helpful. I think what had me
                              > confused at first was my understanding of what L[:] does on either side
                              > of the assignment operator. On the left, it just chooses those elements
                              > and edits them in place; on the right, it makes a copy of that list,
                              > right? (Which I guess is still more or less *doing* the same thing, just
                              > for different purposes)[/color]

                              Interestingly, if it was just a "clear" method nobody would be confused.

                              Comment

                              Working...