title: enumerating all possible strings of length K from an alphabet in Python
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/7074051/is-there-any-best-way-to-generate-all-possible-three-letters-keywords">is there any best way to generate all possible three letters keywords</a>  </p>
  </blockquote>
  
  
  
  <p>how can I enumerate all strings of length K from an alphabet L, where L is simply a list of characters? E.g. if <code>L = ['A', 'B', 'C']</code> and <code>K = 2</code>, I'd like to enumerate all possible strings of length 2 that can be made up with the letters <code>'A'</code>, <code>'B'</code>, <code>'C'</code>. They can be reused, so <code>'AA'</code> is valid.</p>
  
  <p>This is essentially permutations with replacement, as far as I understand. if theres a more correct technical term for this, please let me know.... its essentially all strings of length K that you can make by choosing ANY letter from the alphabet L, and possibly reusing letters, in a way that is sensitive to order (so <code>AB</code> is NOT identical to <code>BA</code> according to this.) is there a clearer way to state this?</p>
  
  <p>in any case i believe the solution is: </p>
  
  <pre><code>[ ''.join(x) for x in product(L, repeat=K) ]
  </code></pre>
  
  <p>but i am interested in other answers to this, esp. naive approaches versus fast Pythonic ones, and discussions of speed considerations.</p>