for-loop over tuple in Python: ValueError
<p>I have a tuple of arrays like the following:</p>

<pre><code>z = (array( [ [1], [2], [3]] ),
     array( [[10],[11],[12]] )
    )
</code></pre>

<p>I want to iterate over them with a simple for loop using two variables:</p>

<pre><code>for x, y in z:
    print("it worked")
    self.doStuff(x, y)
</code></pre>

<p>... but it throws the error:</p>

<pre><code>ValueError: too many values to unpack (expected 2)
</code></pre>

<p>I have googled this error. Many seem to suggest the .split() method or .items(). I'm not really sure, where to add those, since the don't belong to tuples. How can I execute this for-loop properly? Thanks for your help.</p>

<p>The line </p>

<pre><code>for x, y in z:
</code></pre>

<p>assumes (at least in Python2.7), that each element in <code>z</code> can be <a href="https://www.python.org/dev/peps/pep-3113/" rel="nofollow">tuple unpacked</a>. This would be the case if each element in <code>z</code> is, say, a pair tuple:</p>

<pre><code>In [23]: for x, y in [(1, 2), (3, 4)]:
    ...:     pass
    ...: 
</code></pre>

<p>For your case, could it be that you simply want</p>

<pre><code>x, y = z
</code></pre>

<p>?</p>

<p>This works by me:</p>

<pre><code>In [19]: z = (array( [ [1], [2], [3]] ),
    ...:      array( [[10],[11],[12]] )
    ...:     )

In [20]: x, y = z

In [21]: x
Out[21]: 
array([[1],
       [2],
       [3]])

In [22]: y
Out[22]: 
array([[10],
       [11],
       [12]])
</code></pre>

python for-loop