title: Python- find all subwords that can be found inside of a word
body: <p>Ultimately, I want to find out which word in the English dictionary contains the most subwords that are at least three letters.   I wrote this algorithm, but it's too slow to be useful.  Wondering ways I can optimize it</p>
  
  <pre><code>def subWords(word):
      return set((word[0:i] for i in range(2, len(word)+1))) #returns all subWords of length 2 or greater
  
  def checkDict(wordList, dictList):
      return set((word for word in wordList if word in dictList))
  
  def main():
      dictList = [i.strip() for i in open('wordlist.txt').readlines()]
      allwords = list()
      maximum = (0, list())
  
      for dictWords in dictList:
          for i in range (len(dictWords)):
              for a in checkDict(subWords(dictWords[i: len(dictWords) + 1]), dictList):
                  allwords.append(a)
  
          if len(allwords) &gt; maximum[0]:
              maximum = (len(allwords), allwords)
  
          print maximum
          allwords = list()
  
      print maximum 
  main()
  </code></pre>