Re: Producing multiple items in a list comprehension

2008-05-23 Thread Paul Hankin
On May 22, 7:21 pm, "Joel Koltner" <[EMAIL PROTECTED]> wrote: > Is there an easy way to get a list comprehension to produce a flat list of, > say, [x,2*x] for each input argument? > > E.g., I'd like to do something like: > > [ [x,2*x] for x in range(4) ] [x * i for x in xrange(4) for i in xrange(1

Re: Producing multiple items in a list comprehension

2008-05-23 Thread Yves Dorfsman
Peter Otten <[EMAIL PROTECTED]> wrote: > The speed gain is significant. Why should I throw away useful information if > I have it? My thinking was that it wasn't generic enough, and I was looking for a solution that would work for more generic problem. I agree, I shouldn't have used the world "e

Re: Producing multiple items in a list comprehension

2008-05-23 Thread Peter Otten
Yves Dorfsman wrote: > Peter Otten <[EMAIL PROTECTED]> wrote: > >> > A slightly similar problem: If I want to "merge," say, list1=[1,2,3] >> > with list2=[4,5,6] to obtain [1,4,2,5,3,6], is there some clever way >> > with "zip" to do so? > >> >>> items = [None] * 6 >> >>> items[::2] = 1,2,3 >> >

Re: Producing multiple items in a list comprehension

2008-05-22 Thread Yves Dorfsman
Peter Otten <[EMAIL PROTECTED]> wrote: > > A slightly similar problem: If I want to "merge," say, list1=[1,2,3] with > > list2=[4,5,6] to obtain [1,4,2,5,3,6], is there some clever way with "zip" > > to do so? > >>> items = [None] * 6 > >>> items[::2] = 1,2,3 > >>> items[1::2] = 4,5,6 > >>> items

Re: Producing multiple items in a list comprehension

2008-05-22 Thread Joel Koltner
Hi Marc, "Marc Christiansen" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > I'm not sure I would recommend it, but try: > [v for x in range(4) for v in (x, 2 * x)] That certainly works... and it almost seems like a bit less of a hack (if perhaps somewhat harder to read) than the

Re: Producing multiple items in a list comprehension

2008-05-22 Thread Joel Koltner
"inhahe" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > i figured out a solution > > sum([x,2*x] for x in range(4)],[]) #not tested Nice... thanks; I probably had seen code using 'sum' to flatten but hadn't actually understood how it worked. After playing around some it's now cl

Re: Producing multiple items in a list comprehension

2008-05-22 Thread Joel Koltner
"Peter Otten" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] >> A slightly similar problem: If I want to "merge," say, list1=[1,2,3] ... items = [None] * 6 items[::2] = 1,2,3 items[1::2] = 4,5,6 items > [1, 4, 2, 5, 3, 6] Thanks Peter, that's pretty clean -- I lik

Re: Producing multiple items in a list comprehension

2008-05-22 Thread Marc Christiansen
Joel Koltner <[EMAIL PROTECTED]> wrote: > Is there an easy way to get a list comprehension to produce a flat > list of, say, [x,2*x] for each input argument? > > E.g., I'd like to do something like: > > [ [x,2*x] for x in range(4) ] > > ...and receive > > [ 0,0,1,2,2,4,3,6] > > ...but of cours

Re: Producing multiple items in a list comprehension

2008-05-22 Thread Ivan Illarionov
On Thu, 22 May 2008 15:29:42 -0400, inhahe wrote: > "Joel Koltner" <[EMAIL PROTECTED]> wrote in message > news:[EMAIL PROTECTED] >> Is there an easy way to get a list comprehension to produce a flat list >> of, say, [x,2*x] for each input argument? >> >> E.g., I'd like to do something like: >> >>

Re: Producing multiple items in a list comprehension

2008-05-22 Thread inhahe
"Joel Koltner" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Is there an easy way to get a list comprehension to produce a flat list > of, say, [x,2*x] for each input argument? > > E.g., I'd like to do something like: > > [ [x,2*x] for x in range(4) ] > > ...and receive > > [ 0,0

Re: Producing multiple items in a list comprehension

2008-05-22 Thread Peter Otten
Joel Koltner wrote: > Is there an easy way to get a list comprehension to produce a flat list > of, say, [x,2*x] for each input argument? > > E.g., I'd like to do something like: > > [ [x,2*x] for x in range(4) ] > > ...and receive > > [ 0,0,1,2,2,4,3,6] > > ...but of course you really get a

Re: Producing multiple items in a list comprehension

2008-05-22 Thread Gerard flanagan
Joel Koltner wrote: Is there an easy way to get a list comprehension to produce a flat list of, say, [x,2*x] for each input argument? E.g., I'd like to do something like: [ [x,2*x] for x in range(4) ] ...and receive [ 0,0,1,2,2,4,3,6] ...but of course you really get a list of lists: [[0, 0

Producing multiple items in a list comprehension

2008-05-22 Thread Joel Koltner
Is there an easy way to get a list comprehension to produce a flat list of, say, [x,2*x] for each input argument? E.g., I'd like to do something like: [ [x,2*x] for x in range(4) ] ...and receive [ 0,0,1,2,2,4,3,6] ...but of course you really get a list of lists: [[0, 0], [1, 2], [2, 4], [3,