title: Recursive Python function to produce a list of anagrams
body: <p>After a lot of head scratching and googling I still can't figure this out. I'm very new to Python and I'm struggling with the syntax. Conceptually I think I have a pretty decent idea of what I want to do and how to do so recursively. Technically however, coding it into Python however is proving to be a nightmare.</p>
  
  <p>Basically I want to add all of the permutations of a word to list (no duplicate characters allowed), which can then be called by another program or function.</p>
  
  <p>The return command and how to handle white space is really confusing me. I want the recursive function to "return" something once it unwinds but I don't want it to stop the function until all of the characters have iterated and all the permutations have been recursively generated within those iterations. When I run the code below nothing seems to happen.</p>
  
  <pre><code>def permutations(A, B = ''):
      assert len(A) &gt;= 0
      assert len(A) == len(set(A))
      res = []
      if len(A) == 0: res = res.extend(B)
      else:
          for i in range(len(A)):
              permutations(A[0:i] + A[i+1:], B + A[i])
      return res
  
  permutations('word'))
  </code></pre>
  
  <p>If I run the code below it prints out OK to my display pane, but I can't figure out how to get it into an output format that can be used by other program like a list.</p>
  
  <pre><code>def permutations(A, B = ''):
      assert len(A) &gt;= 0
      assert len(A) == len(set(A))
      if len(A) == 0: print(B)
      else:
          for i in range(len(A)):
              permutations(A[0:i] + A[i+1:], B + A[i])
  
  permutations('word')
  </code></pre>
  
  <p>Please could someone advise me on this, while I have some hair left! Very gratefully received. </p>
  
  <p>Thank you</p>
  
  <p>Jon</p>