Assuming you DO mean combinations (no repetitions, order does not matter):
import itertools
S = [ 'a', 'ab', 'ba' ]
for i in range(len(S)+1):
for c in itertools.combinations(S, i):
cc = ''.join(c)
if len(cc) <= 6:
print c
emits all the possibilities:
()
('a',)
('ab',)
('ba',)
('a', 'ab')
('a', 'ba')
('ab', 'ba')
('a', 'ab', 'ba')
If you mean something different than "combinations", it's just an issue of using the right iterator or generator in the for
(e.g., itertools.permutations
, or something else of your own devising).
Edit: if for example you mean "repetitions and order ARE important",
def reps(seq, n):
return itertools.product(*[seq]*n)
for i in range(7):
for c in reps(S, i):
cc = ''.join(c)
if len(cc) <= 6:
print c
will give you the required 85 lines of output.
Edit again: I had the wrong loop limit (and therefore wrong output length) -- tx to the commenter who pointed that out. Also, this approach can produce a string > 1 times, if the ''.join's of different tuples are considered equivalent; e.g., it produces ('a', 'ba') as distinct from ('ab', 'a') although their ''.join is the same (same "word" from different so-called "combinations", I guess -- terminology in use not being entirely clear).