title: List all possible words with n letters
body: <p>I want to list all possible words with n letters where the first letter can be a1 or a2, the second can be b1, b2 or b3, the third can be c1 or c2, ... Here's a simple example input-output for n=2 with each letter having 2 alternatives:</p>
  
  <ul>
  <li>input = [["a","b"],["c","d"]]</li>
  <li>output = ["ac", "ad", "bc", "bd"]</li>
  </ul>
  
  <p>I tried doing this recursively by creating all possible words with the first 2 letters first, so something like this:</p>
  
  <pre><code>def go(l):
      if len(l) &gt; 2:
          head = go(l[0:2])
          tail = l[2:]
          tail.insert(0, head)
          go(tail)
      elif len(l) == 2:
          res = []
          for i in l[0]:
              for j in l[1]:
                  res.append(i+j)
          return res
      elif len(l) == 1:
          return l
      else:
          return None
  </code></pre>
  
  <p>However, this becomes incredibly slow for large n or many alternatives per letter. What would be a more efficient way to solve this?</p>
  
  <p>Thanks</p>