enumerate vs while

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Patrick C
    New Member
    • Apr 2007
    • 54

    enumerate vs while

    Sorry for the simplicity of my questions, but is enumerate a way around using while loops

    my while loop is this:
    Code:
    x = 1
    >>> while x < len(rate):
    ... 	try:
    ... 		growth.append(rate[x])
    ... 		x += 3
    ... 	except IndexError:
    ... 		break
    ...
    thanks
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by Patrick C
    Sorry for the simplicity of my questions, but is enumerate a way around using while loops

    my while loop is this:
    Code:
    x = 1
    >>> while x < len(rate):
    ... 	try:
    ... 		growth.append(rate[x])
    ... 		x += 3
    ... 	except IndexError:
    ... 		break
    ...
    thanks
    Use enumerate() when you need an index number and an item from an iterable:[code=Python]for i, item in enumerate(rates ):
    if not (i-1) % 3:
    growth.append(i tem)[/code]Another form using range():[code=Python]for i in range(1, len(rates), 3):
    growth.append(r ates[i])[/code]

    Comment

    • ghostdog74
      Recognized Expert Contributor
      • Apr 2006
      • 511

      #3
      Originally posted by Patrick C
      Sorry for the simplicity of my questions, but is enumerate a way around using while loops

      my while loop is this:
      Code:
      x = 1
      >>> while x < len(rate):
      ... 	try:
      ... 		growth.append(rate[x])
      ... 		x += 3
      ... 	except IndexError:
      ... 		break
      ...
      thanks
      a better way to create a while loop (IMO) is using infinity loop.
      eg
      Code:
      while 1:
          #statements...
          if some condition:
              break
          if another condition:
              break
      this way, you can customize your loop according to different conditions, whereas in your example, you can only exit while loop on x>len(rate).
      and as to your question, enumerate() works on iterable objects, ie range(), open files, etc.. see here.

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        Originally posted by ghostdog74
        a better way to create a while loop (IMO) is using infinity loop.
        eg
        Code:
        while 1:
            #statements...
            if some condition:
                break
            if another condition:
                break
        this way, you can customize your loop according to different conditions, whereas in your example, you can only exit while loop on x>len(rate).
        <snip>
        I also like the infinite loop implementation and use enumerate() often. That said: Lest our readers get the wrong impression:[CODE=python]while x < len(rate):
        #statements...
        if some condition:
        break
        if another condition:
        break
        [/code]is also valid.

        Comment

        Working...