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>

<h2>Problem 1: updating a tuple</h2>

<p>You cannot modify a tuple. Tuples are immutable!</p>

<pre><code>&gt;&gt;&gt; a = (1, 2, 3)
&gt;&gt;&gt; a[0] = 11
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
TypeError: 'tuple' object does not support item assignment
</code></pre>

<p>You can either replace the whole tuple, or not use tuples at all:</p>

<p><strong>Option 1:</strong></p>

<pre><code>&gt;&gt;&gt; a = (1, 2, 3)
&gt;&gt;&gt; a = (11, 2, 3)
</code></pre>

<p><strong>Option 2:</strong></p>

<pre><code>&gt;&gt;&gt; a = [1, 2, 3]
&gt;&gt;&gt; a[0] = 11
&gt;&gt;&gt; a
[11, 2, 3]
</code></pre>

<h2>Problem two: updating a list in a loop</h2>

<p>Just one list for now. This does not do anything:</p>

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

<p>Try it:</p>

<pre><code>&gt;&gt;&gt; a = [1, 2, 3]
&gt;&gt;&gt;
&gt;&gt;&gt; for loop_a in a:
...     loop_a = 77
...
&gt;&gt;&gt; a
[1, 2, 3]
&gt;&gt;&gt; loop_a
77
</code></pre>

<p>Why? Because <code>for loop_a in a</code> assigns values from <code>a</code> to variable <code>loop_a</code>, then <code>loop_a = some_value</code> assigns some other vaue to <code>loop_a</code>. The only thing ever changed is <code>loop_a</code>.</p>

<p>If you want to change <code>a</code>, you have to access <code>a</code>, for example <code>a[1] = 1</code>.</p>

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

<p>This is not very elegant code, but the way the question is written, that is the thing to do for one list.</p>

<h2>Problem 3: multiple lists</h2>

<p>This is easy.</p>

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

<p>You just have to decide how to calculate <code>number_of_items</code>. That depends on what you want to do.</p>

python arrays for-loop