Re: else condition in list comprehension

2005-01-13 Thread Andrey Tatarinov
Steve Holden wrote: Nick Coghlan wrote: Luis M. Gonzalez wrote: Hi there, I'd like to know if there is a way to add and else condition into a list comprehension. I'm sure that I read somewhere an easy way to do it, but I forgot it and now I can't find it... for example: z=[i+2 for i in range(10

Re: else condition in list comprehension

2005-01-13 Thread Nick Coghlan
Andrey Tatarinov wrote: I presume the point of this is to avoid polluting the local namespace with newval. I further presume you also have plans to do something about i? ;-) no, the point is in grouping definition of newval() with place where it is used. I'd have said the point was both :) But

Re: else condition in list comprehension

2005-01-12 Thread Nick Coghlan
Luis M. Gonzalez wrote: Hi there, I'd like to know if there is a way to add and else condition into a list comprehension. I'm sure that I read somewhere an easy way to do it, but I forgot it and now I can't find it... for example: z=[i+2 for i in range(10) if i%2==0] what if I want i to be i-2

Re: else condition in list comprehension

2005-01-12 Thread Steve Holden
Nick Coghlan wrote: Luis M. Gonzalez wrote: Hi there, I'd like to know if there is a way to add and else condition into a list comprehension. I'm sure that I read somewhere an easy way to do it, but I forgot it and now I can't find it... for example: z=[i+2 for i in range(10) if i%2==0] what if I

Re: else condition in list comprehension

2005-01-12 Thread Stephen Thorne
On 9 Jan 2005 12:20:40 -0800, Luis M. Gonzalez [EMAIL PROTECTED] wrote: Hi there, I'd like to know if there is a way to add and else condition into a list comprehension. I'm sure that I read somewhere an easy way to do it, but I forgot it and now I can't find it... for example: z=[i+2

Re: else condition in list comprehension

2005-01-12 Thread Steven Bethard
Steve Holden wrote: Nick Coghlan wrote: z = [newval(i) for i in range(10)] using: def newval(x): if x % 2: return x - 2 else: return x + 2 Just some more mental twiddling relating to the thread on statement local namespaces. I presume the point of this

Re: else condition in list comprehension

2005-01-11 Thread Nick Coghlan
Dan Bishop wrote: Luis M. Gonzalez wrote: Hi there, I'd like to know if there is a way to add and else condition into a list comprehension. I'm sure that I read somewhere an easy way to do it, but I forgot it and now I can't find it... for example: z=[i+2 for i in range(10) if i%2==0] what if I

Re: else condition in list comprehension

2005-01-11 Thread Anthony
On Mon, 10 Jan 2005 09:13:17 -0700, Steven Bethard [EMAIL PROTECTED] wrote: Luis M. Gonzalez wrote: It's me wrote: z = [i + (2, -2)[i % 2] for i in range(10)] But then why would you want to use such feature? Wouldn't that make the code much harder to understand ... Or are we trying

Re: else condition in list comprehension

2005-01-11 Thread Serhiy Storchaka1659322541
Nick Coghlan wrote: Dan Bishop wrote: Luis M. Gonzalez wrote: Hi there, I'd like to know if there is a way to add and else condition into a list comprehension. I'm sure that I read somewhere an easy way to do it, but I forgot it and now I can't find it... for example: z=[i+2 for i in range(10

Re: else condition in list comprehension

2005-01-10 Thread Luis M. Gonzalez
It's me wrote: z = [i + (2, -2)[i % 2] for i in range(10)] But then why would you want to use such feature? Wouldn't that make the code much harder to understand then simply: z=[] for i in range(10): if i%2: z.append(i-2) else: z.append(i+2) Or are we trying

Re: else condition in list comprehension

2005-01-10 Thread Steven Bethard
Luis M. Gonzalez wrote: It's me wrote: z = [i + (2, -2)[i % 2] for i in range(10)] But then why would you want to use such feature? Wouldn't that make the code much harder to understand then simply: z=[] for i in range(10): if i%2: z.append(i-2) else: z.append(i+2) Or are

else condition in list comprehension

2005-01-09 Thread Luis M. Gonzalez
Hi there, I'd like to know if there is a way to add and else condition into a list comprehension. I'm sure that I read somewhere an easy way to do it, but I forgot it and now I can't find it... for example: z=[i+2 for i in range(10) if i%2==0] what if I want i to be i-2 if i%2 is not equal to 0

Re: else condition in list comprehension

2005-01-09 Thread Dan Bishop
Luis M. Gonzalez wrote: Hi there, I'd like to know if there is a way to add and else condition into a list comprehension. I'm sure that I read somewhere an easy way to do it, but I forgot it and now I can't find it... for example: z=[i+2 for i in range(10) if i%2==0] what if I want i [sic

Re: else condition in list comprehension

2005-01-09 Thread Reinhold Birkenfeld
Matteo Dell'Amico wrote: Luis M. Gonzalez wrote: Hi there, I'd like to know if there is a way to add and else condition into a list comprehension. I'm sure that I read somewhere an easy way to do it, but I forgot it and now I can't find it... for example: z=[i+2 for i in range(10) if i

Re: else condition in list comprehension

2005-01-09 Thread It's me
z = [i + (2, -2)[i % 2] for i in range(10)] But then why would you want to use such feature? Wouldn't that make the code much harder to understand then simply: z=[] for i in range(10): if i%2: z.append(i-2) else: z.append(i+2) Or are we trying to write a book on