Update after notice that you care about order
You need to use itertools.product() and itertools.product() over it:
result = [] for r in range(1, 4): result.extend(''.join(i) for i in product(alphabet, repeat=r))
Simple check:
>>> 'dog' in result True >>> 'god' in result True
Or without a generator expression:
for r in range(1, 4): for i in product(alphabet, repeat=r): result.append(''.join(i))
Tell about the madness
Each solution here cannot handle long words, which means that there are too many possible combinations (this is any mistake). I don't care if this implementation can only process words shorter than "epicalyx", while agf's answer can handle up to 10 letters.
This approach should only be used for small words.
"I want python to list each combination (from 1 letter word to, for example, 1000 word labels)" - This means the OP said.
No one in their right mind should even try to list all of these possibilities in this way, and anyone who thinks it is or tries to push that limit is crazy and should really take a look at ChessMaster's Comments .
source share