title: python all possible permutation using recursion
body: <p>How can I implement the recursive way to write the permutation of an string?
  For example, if the input is <code>'abc'</code>, I want my result to be:    </p>
  
  <pre><code>[a', 'b', 'c', 'd', 'e', 'aa', 'ab', 'ac', 'ad', 'ae', 'ba',
   'bb','bc', 'bd', 'be', 'ca', 'cb', 'cc', 'cd', 'ce', 'da', 'db',
   'dc','dd', 'de', 'ea', 'eb', 'ec', 'ed', 'ee', 'aaa', 'aab', 'aac',
   'aad','aae', 'aba', 'abb', 'abc', 'abd', 'abe', 'aca', 'acb',
   'acc'....]
  </code></pre>
  
  <p>In addition, if a string from the result is also contained in an another list, then return that string. For example <code>'a'</code> and <code>'b'</code> are in <code>[a,aaah,aahed,aahing,aahs,'b']</code>, I want to display the <code>'a'</code> and <code>'b'</code>.</p>
  
  <p><strong>Edit</strong></p>
  
  <p>I tried to use a for loop, but I get a <code>MemoryError</code>.</p>
  
  <pre><code>def perm(l,last,result)
      if len(result[-1]==len(l)):
          return result
      else:
          for i in l:
              for u in last:
                  last.append(u+i)
           result.extend(last)
      perm(l,last,result)
      return result
  
  perm(['a','b','c'],[''],[''])
  </code></pre>