Re: When is List Comprehension inappropriate?

2007-03-21 Thread Paddy
On Mar 19, 2:41 pm, Ben [EMAIL PROTECTED] wrote: I have recently learned how list comprehension works and am finding it extremely cool. I am worried, however, that I may be stuffing it into places that it does not belong. What's the most pythony way to do this: even = [] for x in

Re: When is List Comprehension inappropriate?

2007-03-21 Thread John J. Lee
Steve Holden [EMAIL PROTECTED] writes: Alex Martelli wrote: BJörn Lindqvist [EMAIL PROTECTED] wrote: ... even2 = [(pos, col) for pos, col in iterimage(im, width, height, 2)] list(iterimage(etc etc)) is surely a better way to express identical semantics. More generally, [x for x

Re: When is List Comprehension inappropriate?

2007-03-21 Thread Alex Martelli
Paddy [EMAIL PROTECTED] wrote: ... I have found that I have gone too far when I used listcomps for their sideeffects rather than wanting the list produced, for example the I agree. second listcomp below is an expression as statement I don't want the list produced - just the effect on

Re: When is List Comprehension inappropriate?

2007-03-21 Thread Paddy
On Mar 22, 4:56 am, [EMAIL PROTECTED] (Alex Martelli) wrote: Paddy [EMAIL PROTECTED] wrote: ... I have found that I have gone too far when I used listcomps for their sideeffects rather than wanting the list produced, for example the I agree. second listcomp below is an expression

Re: When is List Comprehension inappropriate?

2007-03-20 Thread bearophileHUGS
BJörn Lindqvist: While they may be faster, Psyco is great here. Also, if you have lots of 2d-loops like for x in something: for y in something:, then it could be more beautiful to separate the iteration from the task: def iterimage(im, width, height, step = 1): for y in range(0, height,

Re: When is List Comprehension inappropriate?

2007-03-20 Thread Steve Holden
Alex Martelli wrote: BJörn Lindqvist [EMAIL PROTECTED] wrote: ... even2 = [(pos, col) for pos, col in iterimage(im, width, height, 2)] list(iterimage(etc etc)) is surely a better way to express identical semantics. More generally, [x for x in whatever] (whether x is a single name or

Re: When is List Comprehension inappropriate?

2007-03-20 Thread Aahz
In article [EMAIL PROTECTED], Alex Martelli [EMAIL PROTECTED] wrote: list(iterimage(etc etc)) is surely a better way to express identical semantics. More generally, [x for x in whatever] (whether x is a single name or gets peculiarly unpacked and repacked like here) is a good example of

Re: When is List Comprehension inappropriate?

2007-03-20 Thread Alex Martelli
Aahz [EMAIL PROTECTED] wrote: In article [EMAIL PROTECTED], Alex Martelli [EMAIL PROTECTED] wrote: list(iterimage(etc etc)) is surely a better way to express identical semantics. More generally, [x for x in whatever] (whether x is a single name or gets peculiarly unpacked and repacked

Re: When is List Comprehension inappropriate?

2007-03-19 Thread Alex Martelli
Ben [EMAIL PROTECTED] wrote: I have recently learned how list comprehension works and am finding it extremely cool. I am worried, however, that I may be stuffing it into places that it does not belong. What's the most pythony way to do this: even = [] for x in range(0,width,2):

Re: When is List Comprehension inappropriate?

2007-03-19 Thread Paul McGuire
On Mar 19, 9:41 am, Ben [EMAIL PROTECTED] wrote: even = [] for x in range(0,width,2): for y in range(0,height,2): color = im.getpixel((x,y)) even.append(((x,y), color)) versus list comprehension: even2 = [((x,y), im.getpixel((x,y))) for x in range(0,width,2) for y in

Re: When is List Comprehension inappropriate?

2007-03-19 Thread BJörn Lindqvist
On 19 Mar 2007 07:41:59 -0700, Ben [EMAIL PROTECTED] wrote: I have recently learned how list comprehension works and am finding it extremely cool. I am worried, however, that I may be stuffing it into places that it does not belong. What's the most pythony way to do this: even = [] for x

Re: When is List Comprehension inappropriate?

2007-03-19 Thread Duncan Booth
Ben [EMAIL PROTECTED] wrote: What's the most pythony way to do this: even = [] for x in range(0,width,2): for y in range(0,height,2): color = im.getpixel((x,y)) even.append(((x,y), color)) versus list comprehension: even2 = [((x,y), im.getpixel((x,y))) for x in

Re: When is List Comprehension inappropriate?

2007-03-19 Thread Steven D'Aprano
On Mon, 19 Mar 2007 07:41:59 -0700, Ben wrote: I have recently learned how list comprehension works and am finding it extremely cool. I am worried, however, that I may be stuffing it into places that it does not belong. Others have suggested reasons why you might or might not want to use

Re: When is List Comprehension inappropriate?

2007-03-19 Thread Alex Martelli
BJörn Lindqvist [EMAIL PROTECTED] wrote: ... even2 = [(pos, col) for pos, col in iterimage(im, width, height, 2)] list(iterimage(etc etc)) is surely a better way to express identical semantics. More generally, [x for x in whatever] (whether x is a single name or gets peculiarly unpacked and