title: Recursive Permutation Generator, swapping list items not working
body: <p>I want to systematically generate permutations of the alphabet. 
  I cannot don't want to use python itertools.permutation, because pregenerating a list of every permutation causes my computer to crash (first time i actually got it to force a shutdown, it was pretty great).</p>
  
  <p>Therefore, my new approach is to generate and test each key on the fly. Currently, I am trying to handle this with recursion.</p>
  
  <p>My idea is to start with the largest list (i'll use a 3 element list as an example), recurse in to smaller list until the list is two elements long. Then, it will print the list, swap the last two, print the list again, and return up one level and repeat.</p>
  
  <p>For example, for 123</p>
  
  <blockquote>
    <p>123 (swap position 0 with position 0)</p>
  
  <pre><code>    23    --&gt; 123 (swap position 1 with position 1)
      32    --&gt; 132 (swap position 1 with position 2)
  </code></pre>
    
    <p>213 (swap position 0 with position 1)</p>
  
  <pre><code>    13    --&gt; 213 (swap position 1 with position 1)
      31    --&gt; 231 (swap position 1 with position 2)
  </code></pre>
    
    <p>321 (swap position 0 with position 2)</p>
  
  <pre><code>    21    --&gt; 321 (swap position 1 with position 1)
      12    --&gt; 312 (swap position 1 with position 2)
  </code></pre>
  </blockquote>
  
  <p>for a four letter number (1234)</p>
  
  <blockquote>
    <p>1234 (swap position 0 with position 0)</p>
  
  <pre><code>    234    (swap position 1 with position 1)
  
             34 --&gt; 1234
             43 --&gt; 1243
      324    (swap position 1 with position 2)
             24 --&gt; 1324
             42 --&gt; 1342
      432    (swap position 1 with position 3)
             32 --&gt; 1432
             23 --&gt; 1423
  </code></pre>
    
    <p>2134 (swap position 0 for position 1)
            134    (swap position 1 with position 1)
                   34 --> 2134
                   43 --> 2143
            314    (swap position 1 with position 2)
                   14--> 2314
                   41--> 2341
            431    (swap position 1 with position 3)
                   31--> 2431
                   13 -->2413</p>
  </blockquote>
  
  <p>This is the code i currently have for the recursion, but its causing me a lot of grief, recursion not being my strong suit. Here's what i have. </p>
  
  <pre><code>def perm(x, y, key):
      print "Perm called: X=",x,", Y=",y,", key=",key
      while (x&lt;y):
  
          print "\tLooping Inward"
  
          print "\t", x," ",y," ", key
          x=x+1
          key=perm(x, y, key)
          swap(x,y,key)
          print "\tAfter 'swap':",x," ",y," ", key, "\n"
  
      print "\nFull Depth Reached"
      #print key, " SWAPPED:? ",swap(x,y,key)
      print swap(x, y, key)
      print " X=",x,", Y=",y,", key=",key
      return key
  
  def swap(x, y, key):
      v=key[x]
      key[x]=key[y]
      key[y]=v
      return key
  </code></pre>
  
  <p>Any help would be greatly appreciated, this is a really cool project and I don't want to abandon it. </p>
  
  <p>Thanks to all! Comments on my method or anything are welcome. </p>