generate all combinations of a list (with variable output length)

Peter Hansen peter at engcorp.com
Fri Jun 28 18:39:29 EDT 2002


thomas wrote:
> 
> hi,
> 
> i'm looking for a way to generate all possible combinations of all items
> in a list. list = ["A","D","$","5","R"]
> 
> but i also want it to use one item several times or not at all and in no
> particular order. the length of the string is 1-10. so a possible output
> would be:
> 
> DD$
> A5RD$D555
> A
> 5RR$$A
> DRRR55$5$A
> ...
> 
> i find it particualy difficult to tell python to use one item several
> times or not at all, and the variable length thing. i don't want to
> reinvent the wheel so someone probably has already a soultion for that
> :) and if not someone can probably push me in some direction.

Why not post your best effort so far and we can critique it?
That way you'll learn a lot more than if someone just hands
you the answer on a silver platter.  We'll also be happier
helping when we know it's not just another homework question
you're supposed to do on your own. ;-)

-Peter



More information about the Python-list mailing list