title: Python: Every possible letter combination
body: <p>What's the most efficient way at producing every possible letter combination of the alphabet?</p>
  
  <p>E.g. a,b,c,d,e...z,aa,ab,ac,ad,ae... ect</p>
  
  <p>I've written a code that successfully produces this result but I feel like it's too inefficient at producing the desired result.</p>
  
  <p>Any ideas?</p>
  
  <p>I've also tried to explain what i've done</p>
  
  <pre><code>#import module time
  import time
  
  #set starting variable "gen", make gen list.
  gen =''
  gen = list(gen)
  
  #expermintal alphabet, not actually used
  alpha ='abcdefghijklmnopqrstuvwxyz'
  alpha = list(alpha)
  
  #Approx. on time taken to complete x letters
  useless_value = raw_input("Press Enter to continue...")
  
  print "How many letters would you like to start at?"
  useless_value = input("&gt; ")
  
  gen = gen*useless_value
  
  #start time for function
  start_time= time.time()
  
  try:
      gen[-1]
  except IndexError:
      print "INVALID LETTER RANGE"
      print "DEFAULT LETTERS USED (1)"
      gen = 'a'
      gen = list(gen)
  
  
  
  #function loop (will never break)
  x=1
  while True:
  
      #print raw string of gen
      print "".join(gen)
  
      #since infinite loop within same function, variables have to be reset
      #thus oh = 0 is reseting for later use
      oh = 0
  
      #change a to b, b to c... ect. Will only make one change
      if gen[-1] =='a':
          gen[-1] ='b'
      elif gen[-1] =='b':
          gen[-1] ='c'
      elif gen[-1] =='c':
          gen[-1] ='d'
      elif gen[-1] =='d':
          gen[-1] ='e'
      elif gen[-1] =='e':
          gen[-1] ='f'
      elif gen[-1] =='f':
          gen[-1] ='g'
      elif gen[-1] =='g':
          gen[-1] ='h'
      elif gen[-1] =='h':
          gen[-1] ='i'
      elif gen[-1] =='i':
          gen[-1] ='j'
      elif gen[-1] =='j':
          gen[-1] ='k'
      elif gen[-1] =='k':
          gen[-1] ='l'
      elif gen[-1] =='l':
          gen[-1] ='m'
      elif gen[-1] =='m':
          gen[-1] ='n'
      elif gen[-1] =='n':
          gen[-1] ='o'
      elif gen[-1] =='o':
          gen[-1] ='p'
      elif gen[-1] =='p':
          gen[-1] ='q'
      elif gen[-1] =='q':
          gen[-1] ='r'
      elif gen[-1] =='r':
          gen[-1] ='s'
      elif gen[-1] =='s':
          gen[-1] ='t'
      elif gen[-1] =='t':
          gen[-1] ='u'
      elif gen[-1] =='u':
          gen[-1] ='v'
      elif gen[-1] =='v':
          gen[-1] ='w'
      elif gen[-1] =='w':
          gen[-1] ='x'
      elif gen[-1] =='x':
          gen[-1] ='y'
  
      #if 'y' changes to 'z'. variable 'oh' is set to '1'.
      # This is so it doesn't enter unwanted equations
      elif gen[-1] =='y':
          gen[-1] ='z'
          oh = 1
  
      #if last string = 'z'
      #Checks max length of gen through [-1,-2].. ect
      if gen[-1] =='z':
  
          #reset s
          s = 0
          x=1
          while True:
  
              try:
                  s
              except NameError:
                  s = -1
              s = s-1
  
              try:
                  gen[s]
              except IndexError:
                  s = s+1
                  break
  
      #s = the amount that gen cannot be
      #Therefore s+1 == max
  
      #resets values because of loop
      d= 0
      q= 0
  
      #this infinite loop checks the string to see if all string values = 'z'
      #if it does it will change all values to 'a' then add 'a'
  
      x=1
      while True:
  
          #useless piece of code #1 
          try:
              d
          except NameError:
              d = 0
  
          #useful
          d = d-1
  
  
          try:
              gen[d]
          except IndexError:
              break
  
          #if d value == 'z' it will continue, otherwise break from loop
          if gen[d] =='z':
  
  
              #if the max == the d value that means all values have been checked
              #and = 'z'. Otherwise it would've already broken the loop
              if s == d:
  
                  x=1
                  while True:
  
  
                      #if oh == 1 which was set from y changing to z
                      # it will not continue
                      #this is so that if 'zy' changes to 'zz' in the first
                      #loop it will not change to 'aaa' while still in the loop
                      # this is so it prints 'zz' and doesn't skip this
                      #This is important to assure all possible combinations are printed
  
                      if oh == 1:
                          break
                      else:
                          pass
  
  
                      #useless q already set to 0
  
                      try:
                          q
                      except NameError:
                          q = 0
  
                      #sets individually all values of 'z' to 'a'
                      q= q-1
                      try:
                          gen[q] ='a'
  
                      #then when q cannot exist, adds an 'a'
                      except IndexError:
                          gen = gen + ['a']
  
                          #sets oh = 1, just in case. most likely useless again
                          oh = 1
                          #prints time taken to complete amount of letters
                          print "Completed", ((len(gen))-1), "letters in", ((time.time() - start_time) /60), "minutes."
                          break
  
              else:
                  continue
  
  
          else:
              break
  
  
      #if the last value = 'z' it will find the next non 'z' value and make all
      #values after it = 'a'. e.g. you have abczezzz
      # it will check find the 'e' and change that 'e' to a 'f'
      # then it will change all the z's after it to a's
      # so final would be - abczfaaa
  
      m = -1            
      if gen[-1] =='z':
  
  
          x=1
          while True:
  
              if oh == 1:
                  break
              else:
                  pass
  
  
              m = m -1
              if gen[m] =='a':
                  gen[m] ='b'
                  gen[(m+1):] = ('a')*(-(m+1))
                  break
  
              elif gen[m] =='b':
                  gen[m] ='c'
                  gen[(m+1):] = ('a')*(-(m+1))
                  break
              elif gen[m] =='c':
                  gen[m] ='d'
                  gen[(m+1):] = ('a')*(-(m+1))
                  break
              elif gen[m] =='d':
                  gen[m] ='e'
                  gen[(m+1):] = ('a')*(-(m+1))
                  break
              elif gen[m] =='e':
                  gen[m] ='f'
                  gen[(m+1):] = ('a')*(-(m+1))
                  break
              elif gen[m] =='f':
                  gen[m] ='g'
                  gen[(m+1):] = ('a')*(-(m+1))
                  break
              elif gen[m] =='g':
                  gen[m] ='h'
                  gen[(m+1):] = ('a')*(-(m+1))
                  break
              elif gen[m] =='h':
                  gen[m] ='i'
                  gen[(m+1):] = ('a')*(-(m+1))
                  break
              elif gen[m] =='i':
                  gen[m] ='j'
                  gen[(m+1):] = ('a')*(-(m+1))
                  break
              elif gen[m] =='j':
                  gen[m] ='k'
                  gen[(m+1):] = ('a')*(-(m+1))
                  break
              elif gen[m] =='k':
                  gen[m] ='l'
                  gen[(m+1):] = ('a')*(-(m+1))
                  break
              elif gen[m] =='l':
                  gen[m] ='m'
                  gen[(m+1):] = ('a')*(-(m+1))
                  break
              elif gen[m] =='m':
                  gen[m] ='n'
                  gen[(m+1):] = ('a')*(-(m+1))
                  break
              elif gen[m] =='n':
                  gen[m] ='o'
                  gen[(m+1):] = ('a')*(-(m+1))
                  break
              elif gen[m] =='o':
                  gen[m] ='p'
                  gen[(m+1):] = ('a')*(-(m+1))
                  break
              elif gen[m] =='p':
                  gen[m] ='q'
                  gen[(m+1):] = ('a')*(-(m+1))
                  break
              elif gen[m] =='q':
                  gen[m] ='r'
                  gen[(m+1):] = ('a')*(-(m+1))
                  break
              elif gen[m] =='r':
                  gen[m] ='s'
                  gen[(m+1):] = ('a')*(-(m+1))
                  break
              elif gen[m] =='s':
                  gen[m] ='t'
                  gen[(m+1):] = ('a')*(-(m+1))
                  break
              elif gen[m] =='t':
                  gen[m] ='u'
                  gen[(m+1):] = ('a')*(-(m+1))
                  break
              elif gen[m] =='u':
                  gen[m] ='v'
                  gen[(m+1):] = ('a')*(-(m+1))
                  break
              elif gen[m] =='v':
                  gen[m] ='w'
                  gen[(m+1):] = ('a')*(-(m+1))
                  break
              elif gen[m] =='w':
                  gen[m] ='x'
                  gen[(m+1):] = ('a')*(-(m+1))
                  break
              elif gen[m] =='x':
                  gen[m] ='y'
                  gen[(m+1):] = ('a')*(-(m+1))
                  break
              elif gen[m] =='y':
                  gen[m] ='z'
                  gen[(m+1):] = ('a')*(-(m+1))
                  break
              elif gen[m] =='z':
                  continue
  </code></pre>