title: Dynamic For Loops in Python
body: <p>i understand that to create dynamic for loops, recursive or itertools module in python is the way to go. Lets say I am doing it in recursive.</p>
  
  <p>What I want is</p>
  
  <pre><code>for var1 in range(var1_lowerlimit, var1_upperlimit, var1_stepsize):
      for var2 in range(var2_lowerlimit, var2_upperlimit, var2_stepsize):
      :
      :
          # do_whatever()
  </code></pre>
  
  <p>repeat for n loops where n is the number of variables</p>
  
  <p>What I have now is I have 2 lists</p>
  
  <pre><code>variable_list = [ var1, var2, var3, ... ]
  boundaries_list = [ [var1_lowerlimit, var1_upperlimit, var1_stepsize], 
                      [var2_lowerlimit, var2_upperlimit, var2_stepsize], ...]
  
  def dynamic_for_loop(variable_list , boundaries_list, no_of_loops, list_index = 0):
      if no_of_loops &lt;= 0:
          # do_whatever()
      else:
          lower_bound = boundaries_list[list_index][0]
          upper_bound = boundaries_list[list_index][1]
          step_size = boundaries_list[list_index][2]
          for index in range(lower_bound, upper_bound, step_size):
              list_index += 1
              try:
                  dynamic_for_loop(variable_list , boundaries_list, no_of_loops - 1, list_index)
              except:
                  list_index = 0
                  dynamic_for_loop(variable_list , boundaries_list, no_of_loops - 1, list_index)
  </code></pre>
  
  <p>I did a reset on list_index as it gets out of range, but i couldn't get the result I want. Can someone enlighten me what went wrong?</p>