Using itertools to create all combinations up to a value

I have this code:

return [reduce(lambda x,y: str(x)+str(y), perm) 
        for perm in itertools.combinations(alphabet, n)]

My problem is I want it to be for all values up to n. It's for homework and I'm having trouble making this a single line Pythonic statement. How would I continue in this manner so I could add a statement like:

 return [reduce(lambda x,y: str(x)+str(y), perm) 
         for perm in itertools.combinations(alphabet, n) for n in range(1,n+1)] 

except one that actually works?

Answers


Something like this?

>>> from itertools import combinations, chain
>>> limit = 10
>>> c = chain(*(combinations(alphabet, x) for x in range(1, limit+1)))
>>> list(c)

I won't display the output, it's too long.

Edit: based on your comments above, it seems like you want the output in the form of strings, so

[''.join(s) for s in chain(*(combinations(alphabet, x) for x in range(1, limit+1)))]

Need Your Help

How can I upgrade specific packages using pip and a requirements file?

django virtualenv pip

I'm using pip with a requirements file, in a virtualenv, for my Django projects. I'm trying to upgrade some packages, notably Django itself, and I'm getting an error about source code conflicts:

Qt5 client certificate authentication

qt ssl client-certificates qnetworkaccessmanager qt5.4

I have Qt 5.4.0 on Windows 8.1 and Qt 5.4.2 on ArchLinux latest and get exactly the same result.