title: How can I infinitely loop an iterator in Python, via a generator or other?
body: <p>It's my understanding that using a Generator is the best way to achieve something like this, but I'm open to suggestions. </p>
  
  <p>Specifically, one use case is this: I'd like to print some items alongside another list, of an arbitrary length, truncating the initial iterator as necessary. </p>
  
  <p>Here is working python code that demonstrates the exact example behavior I desire: </p>
  
  <pre><code>    def loop_list(iterable):
          """
          Return a Generator that will infinitely repeat the given iterable.
  
          &gt;&gt;&gt; l = loop_list(['sam', 'max'])
          &gt;&gt;&gt; for i in range(1, 11):
          ...     print i, l.next()
          ... 
          1 sam
          2 max
          3 sam
          4 max
          5 sam
          6 max
          7 sam
          8 max
          9 sam
          10 max
  
          &gt;&gt;&gt; l = loop_list(['sam', 'max'])
          &gt;&gt;&gt; for i in range(1, 2):
          ...     print i, l.next()
          ... 
          1 sam
          """
          iterable = tuple(iterable)
          l = len(iterable)
          num = 0
          while num &lt; l:
              yield iterable[num]
              num += 1
              if num &gt;= l:
                  num = 0
  </code></pre>
  
  <h2>The Problem / My Question</h2>
  
  <p>As you may have noticed, this only works on lists/tuples/iterables that implement <code>__getitem__</code> (if I'm not mistaken). Ideally, I'd like to be able to pass any iterable, and receive a generator that can properly loop over it's content. </p>
  
  <p>If there's a better way to do something like this <strong>without</strong> a generator, I'm fine with that as well. </p>