itertools.combinations
will give you all combinations of a certain length. We take all the combinations for each possible sublist length. We then map the function you were interested in (the lambda function, or in this case "x".join
) to each of the generated combinations.
>>> import itertools as it
>>> a = ['a','b','c']
>>> l = [map("x".join, list(it.combinations(a, l))) for l in range(1,len(a)+1)]
>>> l
[['a', 'b', 'c'], ['axb', 'axc', 'bxc'], ['axbxc']]
Now l
is a list of lists that we want to flatten:
>>> [ x for y in l for x in y]
['a', 'b', 'c', 'axb', 'axc', 'bxc', 'axbxc']