Is this "code golf"? Can anybody play? Here's another approach:
Define a function like this:
def embiggen(wordlist):
alpha = 'abc'
return [x for x in alpha] + [x+y for x in alpha for y in wordlist]
It makes every word in its input one letter longer in each possible
way, and returns all of the words thus created as well as all of the
one-letter words. (I'm using a very short alphabet, due to the
exponential growth.)
In REPL:
>>> words = []
>>> words = embiggen(words)
>>> words
['a', 'b', 'c']
>>> words = embiggen(words)
>>> words
['a', 'b', 'c', 'aa', 'ab', 'ac', 'ba', 'bb', 'bc', 'ca', 'cb', 'cc']
>>> words = embiggen(words)
>>> words
['a', 'b', 'c', 'aa', 'ab', 'ac', 'aaa', 'aab', 'aac', 'aba', 'abb',
'abc', 'aca', 'acb', 'acc', 'ba', 'bb', 'bc', 'baa', 'bab', 'bac',
'bba', 'bbb', 'bbc', 'bca', 'bcb', 'bcc', 'ca', 'cb', 'cc', 'caa',
'cab', 'cac', 'cba', 'cbb', 'cbc', 'cca', 'ccb', 'ccc']
>>> words = embiggen(words)
>>> len(words)
120
>>>
It's obvious that this could be called from a for-loop with no
modification, which I believe is what the OP wanted.
--
Michael F. Stemper
Deuteronomy 24:17
--
https://mail.python.org/mailman/listinfo/python-list