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 the modulo operator <code>%</code> to index into the proper range:</p>

<pre><code>len_a = len(list_a)
len_b = len(list_b)
end = max(len_a, len_b)
for i in range(end):
    print(list_a[i % len_a], list_b[i % len_b])
    # ... do something else 
</code></pre>

python list circular