Coding style

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

    #1

    Coding style

    Which is better?

    lst = [1,2,3,4,5]

    while lst:
    lst.pop()

    OR

    while len(lst) 0:
    lst.pop()

  • Simon Brunning

    #2
    Re: Coding style

    On 17 Jul 2006 08:56:34 -0700, PTY <ty.2006@yahoo. comwrote:
    Which is better?
    >
    lst = [1,2,3,4,5]
    >
    while lst:
    lst.pop()
    >
    OR
    >
    while len(lst) 0:
    lst.pop()
    How about:

    lst = [1,2,3,4,5]
    while lst:
    lst.pop()

    Or even just:

    lst = []

    ;-)

    --
    Cheers,
    Simon B,
    simon@brunningo nline.net,

    Comment

    • Steve Holden

      #3
      Re: Coding style

      PTY wrote:
      Which is better?
      >
      lst = [1,2,3,4,5]
      >
      while lst:
      lst.pop()
      >
      OR
      >
      while len(lst) 0:
      lst.pop()
      >
      The former, without a doubt. It says exactly the same thing, since lst
      can only be considered false when it is empty. Experienced Python
      programmers would scratch their heads at your second formulation.

      I doubt there's much in it from a time point of view (though I know as I
      write this it will spur someone to use timeit.py to point out I am wrong).

      regards
      Steve
      --
      Steve Holden +44 150 684 7255 +1 800 494 3119
      Holden Web LLC/Ltd http://www.holdenweb.com
      Skype: holdenweb http://holdenweb.blogspot.com
      Recent Ramblings http://del.icio.us/steve.holden

      Comment

      • Tim Chase

        #4
        Re: Coding style

        lst = [1,2,3,4,5]
        while lst:
        lst.pop()
        >
        Or even just:
        >
        lst = []
        Subtly different though...
        >>while lst:
        .... lst.pop()
        ....
        5
        4
        3
        2
        1
        >>lst2
        []
        >>lst = [1,2,3,4,5]
        >>lst2 = lst
        >>lst = []
        >>lst2
        [1, 2, 3, 4, 5]
        >>lst = [1,2,3,4,5]
        >>lst2 = lst
        >>del lst[:]
        >>lst2
        []

        The original while loop changes the actual list, reassigning it
        to a new list prevents other items that reference that list from
        accessing the changes. As shown above, I recommend

        del lst[:]

        which should be as fast as python will let one do it. (maybe?
        again with those timeit guys... ;)

        -tkc




        Comment

        • tac-tics

          #5
          Re: Coding style

          Or even just:
          >
          lst = []
          >
          ;-)
          Indeed.

          I'd say the second one. Empty lists are not false. They are empty. Long
          live dedicated boolean data types.

          Comment

          • dwelch91

            #6
            Re: Coding style

            PTY wrote:
            Which is better?
            >
            lst = [1,2,3,4,5]
            >
            while lst:
            lst.pop()
            >
            OR
            >
            while len(lst) 0:
            lst.pop()
            >
            I think the first one is better, but if all you are doing is removing
            all the items in the list, this is definitely better:

            lst = []

            -Don

            Comment

            • Steve Holden

              #7
              Re: Coding style

              tac-tics wrote:
              >>Or even just:
              >>
              >>lst = []
              >>
              >>;-)
              >
              >
              Indeed.
              >
              I'd say the second one. Empty lists are not false. They are empty. Long
              live dedicated boolean data types.
              >
              Take them off to where they belong!

              I'll bet you still write

              if a>3 == True:

              don't you ;-)

              regards
              Steve
              --
              Steve Holden +44 150 684 7255 +1 800 494 3119
              Holden Web LLC/Ltd http://www.holdenweb.com
              Skype: holdenweb http://holdenweb.blogspot.com
              Recent Ramblings http://del.icio.us/steve.holden

              Comment

              • dwelch91

                #8
                Re: Coding style

                tac-tics wrote:
                >
                I'd say the second one. Empty lists are not false. They are empty. Long
                live dedicated boolean data types.
                >
                Uh, no, empty lists are False in a boolean context:



                -Don

                Comment

                • Peter Otten

                  #9
                  Re: Coding style

                  Steve Holden wrote:
                  I'll bet you still write
                  >
                  if a>3 == True:
                  >
                  don't you ;-)
                  I'll second that.

                  if (a>3) == True:

                  is the correct way :-)

                  Peter

                  Comment

                  • Dave Hansen

                    #10
                    Re: Coding style

                    On Mon, 17 Jul 2006 17:09:32 +0100 in comp.lang.pytho n, "Simon
                    Brunning" <simon@brunning online.netwrote :

                    [...]
                    >
                    >lst = [1,2,3,4,5]
                    >while lst:
                    lst.pop()
                    >
                    >Or even just:
                    >
                    >lst = []
                    >
                    del lst[:]

                    is probably closer to what the OP wants...

                    Regards,
                    -=Dave

                    --
                    Change is inevitable, progress is not.

                    Comment

                    • Roger Miller

                      #11
                      Re: Coding style

                      Peter Otten wrote:
                      Steve Holden wrote:
                      >
                      I'll bet you still write

                      if a>3 == True:

                      don't you ;-)
                      >
                      I'll second that.
                      >
                      if (a>3) == True:
                      >
                      is the correct way :-)
                      >
                      Peter
                      No, to be consistent you'll have to write

                      if ((a>3) == True) == True:

                      Oops, I mean,

                      if (((a>3) == True) == True) == True:

                      Umm, never mind.

                      Comment

                      • rurpy@yahoo.com

                        #12
                        Re: Coding style

                        PTY wrote:
                        Which is better?
                        >
                        lst = [1,2,3,4,5]
                        >
                        while lst:
                        lst.pop()
                        >
                        OR
                        >
                        while len(lst) 0:
                        lst.pop()
                        A dozen posts, but nobody has posted the right
                        answer yet, so I will :-)

                        It doesn't matter -- use whichever you prefer (*)
                        This is an angels on the head of a pin issue.

                        (*) -- If your code is part of an existing body of
                        code that uses one or the other style consistently,
                        then you should do the same.

                        Comment

                        • Donn Cave

                          #13
                          Re: Coding style

                          In article <mailman.8257.1 153158306.27775 .python-list@python.org >,
                          Steve Holden <steve@holdenwe b.comwrote:
                          tac-tics wrote:
                          ....
                          >I'd say the second one. Empty lists are not false. They are empty.
                          >Long live dedicated boolean data types.
                          Take them off to where they belong!
                          Tac-tics is right, an empty list is not False.

                          Anyway, just for some variety, I think (2) is preferrable
                          to (1), as is the following

                          while 1:
                          try:
                          lst.pop()
                          except IndexError:
                          break

                          Rather than blindly apply familiar patterns to our work,
                          I think everyone would agree that coding style in matters
                          like this should follow the underlying point of the code.
                          In this case, the body of the test refers implicitly to
                          the length of the list, since .pop() -(list[a], list[:a])
                          where a is (len(list) - 1) It's therefore quite appropriate
                          for the test to be length.

                          Donn Cave, donn@u.washingt on.edu

                          Comment

                          • Erik Max Francis

                            #14
                            Re: Coding style

                            Donn Cave wrote:
                            Tac-tics is right, an empty list is not False.
                            But that's not what he said. He said it was "not false." That's wrong.
                            It's false. It's just not False.

                            --
                            Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
                            San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
                            The meaning of life is that it stops.
                            -- Franz Kafka

                            Comment

                            • Bob Greschke

                              #15
                              Re: Coding style


                              <rurpy@yahoo.co mwrote in message
                              news:1153168968 .995422.198360@ m73g2000cwd.goo glegroups.com.. .
                              PTY wrote:
                              >Which is better?
                              >>
                              >lst = [1,2,3,4,5]
                              >>
                              >while lst:
                              > lst.pop()
                              >>
                              >OR
                              >>
                              >while len(lst) 0:
                              > lst.pop()
                              >
                              A dozen posts, but nobody has posted the right
                              answer yet, so I will :-)
                              >
                              It doesn't matter -- use whichever you prefer (*)
                              This is an angels on the head of a pin issue.
                              >
                              (*) -- If your code is part of an existing body of
                              code that uses one or the other style consistently,
                              then you should do the same.
                              >
                              I'd go even one step further. Turn it into English (or your favorite
                              non-computer language):

                              1. While list, pop.

                              2. While the length of the list is greater than 0, pop.

                              Which one makes more sense? Guess which one I like. CPU cycles be damned.
                              :)

                              Bob


                              Comment

                              Working...