How to unpack a tuple for looping without being dimension specific
<p>I'd like to do something like this:</p>

<pre><code>    if dim==2:
        a,b=grid_shape
        for i in range(a):
            for j in range(b):
                A[i,j] = ...things...
</code></pre>

<p>where <code>dim</code> is simply the number of elements in my tuple <code>grid_shape</code>. <code>A</code> is a numpy array of dimension <code>dim</code>.
Is there a way to do it without being dimension specific? 
Without having to write ugly code like </p>

<pre><code>    if dim==2:
        a,b=grid_shape
        for i in range(a):
            for j in range(b):
                A[i,j] = ...things...
    if dim==3:
        a,b,c=grid_shape
        for i in range(a):
            for j in range(b):
                for k in range(c):
                    A[i,j,k] = ...things...
</code></pre>

<p>You can catch the rest of the tuple by putting a star in front of the last variable and make a an array by putting parentheses around it.</p>

<pre><code>&gt;&gt;&gt; tupl = ((1, 2), 3, 4, 5, 6)
&gt;&gt;&gt; a, *b = tupl
&gt;&gt;&gt; a
(1, 2)
&gt;&gt;&gt; b
[3, 4, 5, 6]
&gt;&gt;&gt; 
</code></pre>

<p>And then you can loop through b. So it would look something like</p>

<pre><code>a,*b=grid_shape
for i in a:
    for j in range(i):
        for k in b:
            for l in range(k):
                A[j, l] = ...things...
</code></pre>

python loops numpy multidimensional-array tuples