title: Multidimensional Enumeration, how to avoid Code Macro Generation
body: <p>So I have a multidimensional list called Object, with n dimensions</p>
  
  <p>I am performing a procedure for all the elements of this Object (which can be anything including other multidimensional lists)</p>
  
  <p>So I began generating the following inductive way of enumerating the object</p>
  
  <p>Case 1: 1-d (list)</p>
  
  <pre><code>i = 0
  while(i &lt; len(Object)):
      f(Object[i])
      i+=1
  </code></pre>
  
  <p>Case 2: 2-d (list inside list)</p>
  
  <pre><code>i = 0
  while(i &lt; len(Object)):
      j = 0 
      while(j &lt; len(Object[i][j])):
           f(Object[i])
           j+=1
      i+=1
  </code></pre>
  
  <p>At this point it becomes intuitively obvious that the n dimensional object can be traversed through the following code</p>
  
  <pre><code>indexarray = [] #multi dimensional index structure
  i = 0
  while(i &lt; n):
       indexarray.append(0)
       i+=1
  #Prepared the indices
  
  while(indexarray[0] &lt; len(Object)):
      indexarray[1] = 0
      while(indexarray[1] &lt; len(Object[indexarray[0]])):
          indexarray[2] = 0
          while(indexarray[2] &lt; len(Object[indexarray[0]][indexarray[1]])):
              indexarray[3] = 0
                  .
                      .
                          .
                           indexarray[n-1] = 0
                           while(indexarray[n-1] &lt; len(Object[indexarray[0]][...]))
                               f(Object[...])
                               indexarray[n-1]+=1
                           .
                       .
                   .
               indexarray[2]+=1
           indexarray[1]+=1
       indexarray[0]+=1    
  </code></pre>
  
  <p>Except the problem is that I'm going to have to create a subroutine which generates code itself. Personally I think thats awesome! but... perhaps there is a more "elegant" way of doing this. How should one instead proceed?              </p>