I am using an external library named enchant here. My problem is that I guess my program is not returning all possible English words and is slow as well for large string input.I am using both en_US and en_UK here:import enchantimport itertoolsuk = enchant.Dict(en_UK)us=enchant.Dict(en_US)a=[] # stores all possible string combinationsent=input('enter the word: ')ent=ent.rstrip('\r\n')i=3 # minimum length of wordswhile i<=len(ent):  it=itertools.permutations(ent,i)  for y in it:    a.append(.join(y))  i+=1 a.sort(key=len,reverse=True)possible_words=[]for x in a:  if uk.check(x) and x not in possible_words :    possible_words.append(x) for x in a :  if us.check(x) and x not in possible_words:    possible_words.append(x)possible_words.sort(key=len,reverse=True)for x in possible_words:  print(x)Example :enter the word: wsadfghwashwadswagsswagshagdashfadsfagsgashgadshawshagsshawshadwaswadwagsawsadsagashadsfwdfasfadfaggasgadhawhashadhag
Returning all possible English dictionary words that can be formed out of a string
python;optimization;algorithm;performance
1) Since all the permutations produced by itertools.permutations() are unique, there's no need to check for list membership in possible_words. 2) the permutations produced by itertools.permutations() in your (unnecessary) while loop are already ordered by length, so you don't need to sort the results, merely reverse them.3) you can combine the US and UK dictionary lookups into one if statement.In fact, it's possible to compress the entire lower half of your code comfortably into two lines:word_perms = (.join(j) for k in range(1, len(ent) + 1) for j in permutations(ent, k))possible_words = reversed([i for i in word_perms if us.check(i) or uk.check(i)])