How to loop through 3 different arrays by using a single for loop in Python
<p>I have 3 different arrays:</p>

<pre><code>a = (x, y, z)
b = (p, q)
c = (r, s)
</code></pre>

<p>I need to loop through all this arrays. Presently I am using the below method:</p>

<pre><code>for loop_a in a:
  loop_a = some_value

for loop_b in b:
  loop_b = some_value

for loop_c in c:
  loop_c = some_value
</code></pre>

<p>How I can do this using a single for loop and iterate through all the arrays. Functionality inside the loop is same for all. I need an efficient way to do this. I checked many other already answered questions, but couldn't find a fitting answer.</p>

<p>There are two ways. The more traditional way is to use an index and a for loop. In this case, it's assumed that all lists are the same length and gets that length from the first item:</p>

<pre><code>for i in range(len(a)):
    a[i] = some_value
    b[i] = some_value
    c[i] = some_value
</code></pre>

<p>Python, however, has a convenience function called <code>zip()</code> that takes multiple lists and iterates them all the same time, returning a tuple.</p>

<pre><code>for values in zip(a, b, c):
    values[0] = some_value # sets value for a
    values[1] = some_value # sets value for b
    values[2] = some_value # sets value for c
</code></pre>

<p>This can be made more readable by unrolling the tuple:</p>

<pre><code>for a_item, b_item, c_item in zip(a, b, c):
    a_item = some_value
    b_item = some_value
    c_item = some_value
</code></pre>

<p>Zipping doesn't need lists to be the same length and will add <code>None</code> for any missing values.</p>

python arrays for-loop