Is using range() in for loops really Pythonic?

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

    Is using range() in for loops really Pythonic?

    I know it's popular and very handy, but I'm curious if there are purists
    out there who think that using something like:

    for x in range(10):
    #do something 10 times

    is unPythonic. The reason I ask is because the structure of the for loop
    seems to be for iterating through a sequence. It seems somewhat
    artificial to use the for loop to do something a certain number of
    times, like above.

    Anyone out there refuse to use it this way, or is it just impossible to
    avoid?
  • Matt Nordhoff

    #2
    Re: Is using range() in for loops really Pythonic?

    John Salerno wrote:
    I know it's popular and very handy, but I'm curious if there are purists
    out there who think that using something like:
    >
    for x in range(10):
    #do something 10 times
    >
    is unPythonic. The reason I ask is because the structure of the for loop
    seems to be for iterating through a sequence. It seems somewhat
    artificial to use the for loop to do something a certain number of
    times, like above.
    >
    Anyone out there refuse to use it this way, or is it just impossible to
    avoid?
    Well, you should use "xrange(10) " instead of "range(10)" . While range()
    returns a list of every single number, xrange() returns an iterable
    object that only generates them on-demand, so xrange(1) and
    xrange(sys.maxi nt) will use the same amount of RAM.

    (Not that it matters when it's only 10 items, of course.)

    Other than that, "for i in xrange(10)" is a standard Python idiom, even
    though it is a bit weird.
    --

    Comment

    • 7stud

      #3
      Re: Is using range() in for loops really Pythonic?

      On May 10, 8:19 pm, John Salerno <johnj...@NOSPA Mgmail.comwrote :
      I know it's popular and very handy, but I'm curious if there are purists
      out there who think that using something like:
      >
      for x in range(10):
          #do something 10 times
      >
      is unPythonic. The reason I ask is because the structure of the for loop
      seems to be for iterating through a sequence. It seems somewhat
      artificial to use the for loop to do something a certain number of
      times, like above.
      >
      x = range(10)
      print x

      --output:--
      [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

      Comment

      • Paddy

        #4
        Re: Is using range() in for loops really Pythonic?

        On May 11, 3:19 am, John Salerno <johnj...@NOSPA Mgmail.comwrote :
        I know it's popular and very handy, but I'm curious if there are purists
        out there who think that using something like:
        >
        for x in range(10):
        #do something 10 times
        >
        is unPythonic. The reason I ask is because the structure of the for loop
        seems to be for iterating through a sequence. It seems somewhat
        artificial to use the for loop to do something a certain number of
        times, like above.
        >
        Anyone out there refuse to use it this way, or is it just impossible to
        avoid?
        Hi John,
        If you have an object that is both indexable and iterable, then
        visiting every member by first generating an index then indexing the
        object is un-pythonic. You should just iterate over the object.

        Like most rules, things can get fuzzy around the edges: if you have n
        objects to be visited each index at a time, then the more functional
        approach would be to izip all the objects and iterate over the result.
        Another way would be to iterate over the the enumeration of one object
        and use the index created to index the other n-1 objects.

        In all such situations you need to remember that things such as code
        clarity, and speed, might make the final decision for you.


        In the following examples then the first is what I would use, izip. If
        I needed an index then I'd prefer the last, which combines izip and
        enumerate:

        PythonWin 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
        (Intel)] on win32.
        Portions Copyright 1994-2006 Mark Hammond - see 'Help/About PythonWin'
        for further copyright information.
        >>obj1 = 'CSM'
        >>obj2 = 'aue'
        >>obj3 = 'tmn'
        >>from itertools import izip
        >>for x,y,z in izip(obj1, obj2, obj3):
        ... print x,y,z
        ...
        C a t
        S u m
        M e n
        >>for i,x in enumerate(obj1) :
        ... print x, obj2[i], obj3[i]
        ...
        C a t
        S u m
        M e n
        >>for i in range(len(obj1) ):
        ... print obj1[i], obj2[i], obj3[i]
        ...
        C a t
        S u m
        M e n
        >>for i,(x,y,z) in enumerate(izip( obj1, obj2, obj3)):
        ... print i, x, y, z
        ...
        0 C a t
        1 S u m
        2 M e n
        >>>

        - Paddy.

        Comment

        • Paul Rubin

          #5
          Re: Is using range() in for loops really Pythonic?

          John Salerno <johnjsal@NOSPA Mgmail.comwrite s:
          for x in range(10):
          #do something 10 times
          >
          is unPythonic. The reason I ask is because the structure of the for loop
          seems to be for iterating through a sequence. It seems somewhat
          artificial to use the for loop to do something a certain number of
          times, like above.
          It is pretty natural in imperative-style code. The one thing I'd do
          differently is use xrange instead of range, to avoid creating a
          10-element list in memory before starting the loop.

          Comment

          • XLiIV

            #6
            Re: Is using range() in for loops really Pythonic?

            On May 11, 4:19 am, John Salerno <johnj...@NOSPA Mgmail.comwrote :
            I know it's popular and very handy, but I'm curious if there are purists
            out there who think that using something like:
            >
            for x in range(10):
                #do something 10 times
            >
            is unPythonic. The reason I ask is because the structure of the for loop
            seems to be for iterating through a sequence. It seems somewhat
            artificial to use the for loop to do something a certain number of
            times, like above.
            >
            Anyone out there refuse to use it this way, or is it just impossible to
            avoid?
            The range() function returns a list and list is a sequence, isn't?

            Comment

            • castironpi@gmail.com

              #7
              Re: Is using range() in for loops really Pythonic?

              On May 11, 12:38 am, Paul Rubin <http://phr...@NOSPAM.i nvalidwrote:
              John Salerno <johnj...@NOSPA Mgmail.comwrite s:
              for x in range(10):
                  #do something 10 times
              >
              is unPythonic. The reason I ask is because the structure of the for loop
              seems to be for iterating through a sequence. It seems somewhat
              artificial to use the for loop to do something a certain number of
              times, like above.
              >
              It is pretty natural in imperative-style code.  The one thing I'd do
              differently is use xrange instead of range, to avoid creating a
              10-element list in memory before starting the loop.
              If you give extras to memory, can Python live?

              Comment

              • John Salerno

                #8
                Re: Is using range() in for loops really Pythonic?

                John Salerno wrote:
                I know it's popular and very handy, but I'm curious if there are purists
                out there who think that using something like:
                >
                for x in range(10):
                #do something 10 times
                >
                is unPythonic. The reason I ask is because the structure of the for loop
                seems to be for iterating through a sequence. It seems somewhat
                artificial to use the for loop to do something a certain number of
                times, like above.
                >
                Anyone out there refuse to use it this way, or is it just impossible to
                avoid?
                Ok, I think most people are misunderstandin g my question a little. I
                probably should have said xrange instead of range, but the point of my
                question remains the same:

                Should a for loop be used simply to do something X number of times, or
                should it strictly be used to step through an iterable object for the
                purpose of processing the items in that object?

                Comment

                • Terry Reedy

                  #9
                  Re: Is using range() in for loops really Pythonic?


                  "XLiIV" <Tymoteusz.Jank owski@gmail.com wrote in message
                  news:41078222-aec0-4e8b-8a1f-945cdf814498@m7 3g2000hsh.googl egroups.com...
                  On May 11, 4:19 am, John Salerno <johnj...@NOSPA Mgmail.comwrote :
                  I know it's popular and very handy, but I'm curious if there are purists
                  out there who think that using something like:
                  >
                  for x in range(10):
                  #do something 10 times
                  >
                  is unPythonic. The reason I ask is because the structure of the for loop
                  seems to be for iterating through a sequence. It seems somewhat
                  artificial to use the for loop to do something a certain number of
                  times, like above.
                  >
                  Anyone out there refuse to use it this way, or is it just impossible to
                  avoid?
                  |The range() function returns a list and list is a sequence, isn't?

                  yes, for loops iterate thru any iterable



                  Comment

                  • John Salerno

                    #10
                    Re: Is using range() in for loops really Pythonic?

                    XLiIV wrote:
                    The range() function returns a list and list is a sequence, isn't?
                    I think you're missing the point. To me, there seems to be a fundamental
                    difference between these two things:

                    ---

                    people = ['Sam', 'Bob', 'Fred']

                    for name in people:
                    print name

                    ---

                    AND

                    ---

                    num = 33

                    for x in xrange(10):
                    print num += 1

                    ---

                    To me, the first example is a pure use of the for loop. You are
                    iterating through an object and *using* the items you are stepping through.

                    The second example, however, is simply doing something 10 times, and
                    what it's doing has nothing to do with 'x' or xrange. So it seems like
                    an abuse of the for loop.

                    Comment

                    • Arnaud Delobelle

                      #11
                      Re: Is using range() in for loops really Pythonic?

                      John Salerno <johnjsal@gmail NOSPAM.comwrite s:
                      John Salerno wrote:
                      >I know it's popular and very handy, but I'm curious if there are purists
                      >out there who think that using something like:
                      >>
                      >for x in range(10):
                      > #do something 10 times
                      >>
                      >is unPythonic. The reason I ask is because the structure of the for loop
                      >seems to be for iterating through a sequence. It seems somewhat
                      >artificial to use the for loop to do something a certain number of
                      >times, like above.
                      >>
                      >Anyone out there refuse to use it this way, or is it just impossible to
                      >avoid?
                      >
                      Ok, I think most people are misunderstandin g my question a little. I
                      probably should have said xrange instead of range, but the point of my
                      question remains the same:
                      >
                      Should a for loop be used simply to do something X number of times, or
                      should it strictly be used to step through an iterable object for the
                      purpose of processing the items in that object?
                      It makes me feel slightly uncomfortable too, but AFAIK there is no
                      better way in Python. What I sometimes do is make the for loop bind
                      its variable to something useful instead of an unused counter.
                      Contrived example:

                      # Print 'hello' 10 times; x is not used
                      for x in xrange(10):
                      print 'hello'

                      # By changing what is iterated over, no unused variable:
                      from itertools import repeat
                      for msg in repeat('hello', 10):
                      print msg

                      --
                      Arnaud

                      Comment

                      • Ben Finney

                        #12
                        Re: Is using range() in for loops really Pythonic?

                        John Salerno <johnjsal@gmail NOSPAM.comwrite s:
                        num = 33
                        >
                        for x in xrange(10):
                        print num += 1
                        Which is better done by 'num += 10'.

                        Can you come up with an example that isn't trivially replaced with
                        clearer code? That might make it clearer what your concern is.
                        The [above] example [...] is simply doing something 10 times, and
                        what it's doing has nothing to do with 'x' or xrange. So it seems
                        like an abuse of the for loop.
                        In such cases, the name 'dummy' is conventionally bound to the items
                        from the iterator, for clarity of purpose::

                        for dummy in range(10):
                        # do stuff that makes no reference to 'dummy'

                        Also note that 'range' will return an iterator (not a list) in Python
                        3.0, and 'xrange' is removed since it's then obsolete
                        <URL:http://www.python.org/dev/peps/pep-3100/#built-in-namespace>.

                        --
                        \ “Isn’t it enough to see that a garden is beautiful without |
                        `\ having to believe that there are fairies at the bottom of it |
                        _o__) too?” —Douglas Adams |
                        Ben Finney

                        Comment

                        • Yves Dorfsman

                          #13
                          Re: Is using range() in for loops really Pythonic?

                          John Salerno wrote:
                          >
                          To me, the first example is a pure use of the for loop. You are
                          iterating through an object and *using* the items you are stepping through.
                          >
                          The second example, however, is simply doing something 10 times, and
                          what it's doing has nothing to do with 'x' or xrange. So it seems like
                          an abuse of the for loop.
                          Well, I would say this:
                          myl = ['a', 'b', 'c', 'd']
                          for i in xrange(len(myl) ):
                          print myl[i]

                          As you would see in other languages, is an abuse in python. But in you need
                          to iterate 5 times over something. Do you have a cleaner / python'er
                          alternative ?

                          Do you find the following cleaner:

                          x = 0
                          while x <= 4:
                          print x
                          x += 1



                          Yves.
                          Calgary AIX Linux UNIX React TypeScript JavaScript python contractor consultant programmer Yves Dorfsman

                          Comment

                          • Jonathsn Cronin

                            #14
                            Re: Is using range() in for loops really Pythonic?

                            On Sun, 11 May 2008 16:16:53 -0400, John Salerno wrote
                            (in message <48275446$0$116 28$607ed4bc@cv. net>):
                            XLiIV wrote:
                            >
                            >The range() function returns a list and list is a sequence, isn't?
                            >
                            I think you're missing the point. To me, there seems to be a fundamental
                            difference between these two things:
                            >
                            ---
                            >
                            people = ['Sam', 'Bob', 'Fred']
                            >
                            for name in people:
                            print name
                            >
                            ---
                            >
                            AND
                            >
                            ---
                            >
                            num = 33
                            >
                            for x in xrange(10):
                            print num += 1
                            >
                            ---
                            >
                            To me, the first example is a pure use of the for loop. You are
                            iterating through an object and *using* the items you are stepping through.
                            >
                            The second example, however, is simply doing something 10 times, and
                            what it's doing has nothing to do with 'x' or xrange. So it seems like
                            an abuse of the for loop.
                            I agree in principle; the first is iteration and the second is repetition.
                            In Python, the common idiom for a fixed number of repetitions is iterating
                            over a number range. This is true in most languages I am familiar with,
                            probably because fixed repetition, where you don't care about the "index"
                            value, is rarely used.

                            The only language I've used that does have fixed repetition is an (old)
                            dialect of lisp, and I'm not sure it even made it into Common Lisp.
                            Smalltalk and Ruby do have fixed repetition.

                            Using range may not be entirely elegant, but is pragmatic, and not, to me,
                            particularly ugly. In Python, unlike some languages, you don't have to
                            declare the x.

                            I think you could add it without too much change to the parsing.

                            for <expression>:
                            <block>

                            Seeing a ":" instead of "in" would mean a repetition statement which would
                            be interpreted as:
                            -- if <expressioneval uates to an integer, execute the block that number of
                            times.
                            -- If <expressioneval uates to an iterator, execute the block until the
                            iterator is exhausted.

                            Even if possible, I see this at best a minor improvement and more likely a
                            negative because the for keyword is now overloaded. (see "static" in C/Java.)
                            You could add a new keyword but the benefit here is much to small to justify
                            the trouble.

                            Jonathan

                            Comment

                            • Carl Banks

                              #15
                              Re: Is using range() in for loops really Pythonic?

                              On May 11, 6:44 pm, Ben Finney <bignose+hate s-s...@benfinney. id.au>
                              wrote:
                              In such cases, the name 'dummy' is conventionally bound to the items
                              from the iterator, for clarity of purpose::
                              >
                              for dummy in range(10):
                              # do stuff that makes no reference to 'dummy'
                              Is this documented? I've never heard of this convention. It's not
                              PEP 8, and I've never seen consistent usage of any name. I'd be
                              interested in knowing where you read that this was a convention, or in
                              what subcommunities it's a convention in.

                              I think dummy is a terrible name to use for this, since in no other
                              usage I can think of does the word "dummy" suggest something isn't
                              used. In fact, quite the opposite. For example, the dummy_threads
                              module is most definitely used; the word dummy means that it's
                              stripped down. Based on that, your usage of the symbol dummy above
                              would suggest to me that it's a value used in lieu of something else
                              (such as a calculated value).

                              In mathematics, a dummy argument another name for the independent
                              variable of a function (or more accurately, the symbol used to
                              represent it), which also doesn't match your usage.

                              If a value isn't used, then I think the most clear name for it is
                              "unused".


                              Carl Banks

                              Comment

                              Working...