Creating unique combinations from lists

breal hacker.stevenson at gmail.com
Wed Jan 16 14:44:23 EST 2008


On Jan 16, 11:33 am, "Reedick, Andrew" <jr9... at ATT.COM> wrote:
> > -----Original Message-----
> > From: python-list-bounces+jr9445=att.... at python.org [mailto:python-
> > list-bounces+jr9445=att.... at python.org] On Behalf Of breal
> > Sent: Wednesday, January 16, 2008 2:15 PM
> > To: python-l... at python.org
> > Subject: Creating unique combinations from lists
>
> > I have three lists... for instance
>
> > a = ['big', 'small', 'medium'];
> > b = ['old', 'new'];
> > c = ['blue', 'green'];
>
> > I want to take those and end up with all of the combinations they
> > create like the following lists
> > ['big', 'old', 'blue']
> > ['small', 'old', 'blue']
> > ['medium', 'old', 'blue']
> > ['big', 'old', 'green']
> > ['small', 'old', 'green']
> > ['medium', 'small', 'green']
> > ['big', 'new', 'blue']
> > ['small', 'new', 'blue']
> > ['medium', 'new', 'blue']
> > ['big', 'new', 'green']
> > ['small', 'new', 'green']
> > ['medium', 'new', 'green' ]
>
> > I could do nested for ... in loops, but was looking for a Pythonic way
> > to do this.  Ideas?
>
> http://www.python.org/dev/peps/pep-0202/

Thanks for the reply.  I never realized you could use list
comprehension like this... AWESOME!



More information about the Python-list mailing list