Iterating Through List or Tuple

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

    Iterating Through List or Tuple

    Is there a way to loop or iterate through a list/tuple in such a way
    that when you reach the end, you start over at the beginning? For
    example, suppose I define a list "daysOfWeek " such that:
    >>daysOfWeek = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday']
    If today is Sunday, I can set the variable "day" to today by:
    >>i = iter(daysOfWeek )
    >>day = i.next()
    >>print day
    sunday

    If I want to find out the day of the week 2 days from now, then this
    code works ok:
    >>for x in xrange(2): day = i.next()
    >>print day
    tuesday

    However, when extending my range beyond the number of items in the
    list, I receive an error. For example, if I want to find out the day
    of the week 11 days from today, I get this:
    >>for x in xrange(11): day = i.next()

    Traceback (most recent call last):
    File "<pyshell#8 7>", line 1, in <module>
    for x in xrange(11): day = i.next()
    StopIteration

    Is there a way to easily loop through a list or tuple (and starting
    over at the beginning when reaching the end) without having to resort
    to an "if" or "while" statement?

    (My question concerns the more general use of lists and tuples, not
    necessarily determining days of the week. I know about using "import
    datetime" and "from calendar import weekday" but thought that using
    the days of the week would best illustrate my problem.)

    As always, thanks in advance.

    Samir
  • Fredrik Lundh

    #2
    Re: Iterating Through List or Tuple

    Samir wrote:
    Is there a way to loop or iterate through a list/tuple in such a way
    that when you reach the end, you start over at the beginning? For
    example, suppose I define a list "daysOfWeek " such that:
    >
    >>>daysOfWeek = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday']
    >>import itertools
    >>help(itertool s.cycle)
    Help on class cycle in module itertools:

    class cycle(__builtin __.object)
    | cycle(iterable) --cycle object
    |
    | Return elements from the iterable until it is exhausted.
    | Then repeat the sequence indefinitely.

    ....
    >>L = [1, 2, 3]
    >>for i, x in enumerate(itert ools.cycle(L)):
    .... print i, x
    .... if i >= 10:
    .... break
    ....
    0 1
    1 2
    2 3
    3 1
    4 2
    5 3
    6 1
    7 2
    8 3
    9 1
    10 2
    >>>
    </F>

    Comment

    • Marc 'BlackJack' Rintsch

      #3
      Re: Iterating Through List or Tuple

      On Tue, 22 Jul 2008 14:43:10 -0700, Samir wrote:
      Is there a way to loop or iterate through a list/tuple in such a way
      that when you reach the end, you start over at the beginning? For
      example, suppose I define a list "daysOfWeek " such that:
      >
      >>>daysOfWeek = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday']
      >
      If today is Sunday, I can set the variable "day" to today by:
      >
      >>>i = iter(daysOfWeek )
      >>>day = i.next()
      >>>print day
      sunday
      >
      If I want to find out the day of the week 2 days from now, then this
      code works ok:
      >
      >>>for x in xrange(2): day = i.next()
      >
      >>>print day
      tuesday
      >
      However, when extending my range beyond the number of items in the
      list, I receive an error. For example, if I want to find out the day
      of the week 11 days from today, I get this:
      >
      >>>for x in xrange(11): day = i.next()
      >
      >
      Traceback (most recent call last):
      File "<pyshell#8 7>", line 1, in <module>
      for x in xrange(11): day = i.next()
      StopIteration
      >
      Is there a way to easily loop through a list or tuple (and starting
      over at the beginning when reaching the end) without having to resort
      to an "if" or "while" statement?
      For sequences: days_of_week[(today + offset) % len(days_of_wee k)]

      For iterables in general: `itertools.cycl e()`

      Ciao,
      Marc 'BlackJack' Rintsch

      Comment

      • Larry Bates

        #4
        Re: Iterating Through List or Tuple

        Samir wrote:
        Is there a way to loop or iterate through a list/tuple in such a way
        that when you reach the end, you start over at the beginning? For
        example, suppose I define a list "daysOfWeek " such that:
        >
        >>>daysOfWeek = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday']
        >
        If today is Sunday, I can set the variable "day" to today by:
        >
        >>>i = iter(daysOfWeek )
        >>>day = i.next()
        >>>print day
        sunday
        >
        If I want to find out the day of the week 2 days from now, then this
        code works ok:
        >
        >>>for x in xrange(2): day = i.next()
        >
        >>>print day
        tuesday
        >
        However, when extending my range beyond the number of items in the
        list, I receive an error. For example, if I want to find out the day
        of the week 11 days from today, I get this:
        >
        >>>for x in xrange(11): day = i.next()
        >
        >
        Traceback (most recent call last):
        File "<pyshell#8 7>", line 1, in <module>
        for x in xrange(11): day = i.next()
        StopIteration
        >
        Is there a way to easily loop through a list or tuple (and starting
        over at the beginning when reaching the end) without having to resort
        to an "if" or "while" statement?
        >
        (My question concerns the more general use of lists and tuples, not
        necessarily determining days of the week. I know about using "import
        datetime" and "from calendar import weekday" but thought that using
        the days of the week would best illustrate my problem.)
        >
        As always, thanks in advance.
        >
        Samir
        >>import itertools
        >>i = itertools.cycle (daysOfWeek)
        >>i.next()
        'sunday'
        >>i.next()
        'monday'
        ..
        ..
        ..
        >>i.next()
        'saturday'
        >>i.next()
        'sunday'

        -Larry

        Comment

        • Samir

          #5
          Re: Iterating Through List or Tuple

          Fredrik, Marc, Larry -- Thank you all for your very fast and
          informative replies. I had not come across "itertools" in my search.
          This group is a great resource.

          Samir

          Comment

          Working...