How to access items from two lists in circular order?
<p>I have 2 lists,</p>

<pre><code>list_a = ['color-1', 'color-2', 'color-3', 'color-4']
list_b = ['car1', 'car2', 'car3', 'car4' ........... 'car1000']
</code></pre>

<p>I need to access the elements in a circular order of <code>list_a</code>:</p>

<pre><code>['color-1']['car1']
['color-2']['car2']
['color-3']['car3']
['color-4']['car4']
['color-1']['car5']  #list_a is starting from color-1 once it reaches end
['color-2']['car6']  #... goes on until end of items in list_b
</code></pre>

<p>I tried this, it doesn't work. Please advise.</p>

<pre><code>start=0
i=0
for car_idx in xrange(start, end):
    if i &lt;= len(color_names):
        try:
            self.design(color_names[i], self.cars[car_idx])
            i+=1
        except SomeException as exe:
            print 'caught an error'
</code></pre>

<p>Use <a href="https://docs.python.org/2/library/itertools.html#itertools.cycle"><code>itertools.cycle</code></a> to make a cyclic iterable out of <code>list_a</code>.
Use <a href="https://docs.python.org/3/library/functions.html#zip"><code>zip</code></a> to pair items from the cyclic iterable with items from <code>list_b</code>. The iterable returned by <code>zip</code> will stop when the shortest of the iterables passed to <code>zip</code> (i.e. <code>list_b</code>) ends.</p>

<pre><code>import itertools as IT
list_a = ['color-1', 'color-2', 'color-3', 'color-4']
list_b = ['car1', 'car2', 'car3', 'car4', 'car5', 'car6', 'car1000']

for a, b in zip(IT.cycle(list_a), list_b):
    print(a, b)
</code></pre>

<p>prints</p>

<pre><code>color-1 car1
color-2 car2
color-3 car3
color-4 car4
color-1 car5
color-2 car6
color-3 car1000
</code></pre>

python list circular