Velocity Reviews > else condition in list comprehension

# else condition in list comprehension

Anthony
Guest
Posts: n/a

 01-11-2005
On Mon, 10 Jan 2005 09:13:17 -0700, Steven Bethard
<(E-Mail Removed)> 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 to write a book on "Puzzles in Python"?

> >
> > Once you get used to list comprehensions (and it doesn't take long),
> > they are a more concise and compact way to express these operations.

>
> After looking the two suggestions over a couple of times, I'm still
> undecided as to which one is more readable for me. The problem is not
> the list comprehensions (which I love and use extensively). The problem
> is the odd syntax that has to be used for an if/then/else expression in
> Python.

They're both pretty unreadable, IMHO. Why not just factor out the
if/then/else function like this:

..def plusMinusTwo(i):
.. if i%2 == 0:
.. return i-2
.. else:
.. return i+2
..
..z = [plusMinusTwo(i) for i in range(10)]

Then you can add whatever you like into the function.

Anthony

--
-----------------------------------------------------
HyPEraCtiVE? HeY, WhO aRE YoU cALliNg HypERaCtIve?!
http://www.velocityreviews.com/forums/(E-Mail Removed)
-----------------------------------------------------

Serhiy Storchaka1659322541
Guest
Posts: n/a

 01-11-2005
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) if i%2==0]
>>> what if I want i [sic] to be "i-2" if i%2 is not equal to 0?

>>
>>
>>
>> z = [i + (2, -2)[i % 2] for i in range(10)]

>
>
> For the specific case of +/- a number, (-1) ** x works, too:
>
> z = [i + 2 * ((-1) ** i) for i in range(10)]
>
> Not that I'm claiming it's particularly readable or anything. . . just
> that it works

Yet another variant:
z = [i + ( (i % 2) and -2 or 2 ) for i in range(10)]

--
Serhiy Storchaka

Nick Coghlan
Guest
Posts: n/a

 01-12-2005
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" if i%2 is not equal to 0?
>

Hmm:

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.

Cheers,
Nick.

--
Nick Coghlan | (E-Mail Removed) | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net

Steve Holden
Guest
Posts: n/a

 01-12-2005
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 want i to be "i-2" if i%2 is not equal to 0?
>>

>
> Hmm:
>
> 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 is to avoid polluting the local namespace
with "newval". I further presume you also have plans to do something

regards
Steve
--
Steve Holden http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/
Holden Web LLC +1 703 861 4237 +1 800 494 3119

Stephen Thorne
Guest
Posts: n/a

 01-12-2005
On 9 Jan 2005 12:20:40 -0800, Luis M. Gonzalez <(E-Mail Removed)> 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" if i%2 is not equal to 0?

z = [i+2-(i%2)*4 for i in range(10)]

C'mon, who needs an 'if' statement when we have maths!

Stephen.

Steven Bethard
Guest
Posts: n/a

 01-13-2005
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 is to avoid polluting the local namespace
> with "newval". I further presume you also have plans to do something

Well, while I'm not at all a fan of the "using" syntax, getting rid of
'i' is simple:

z = list(newval(i) for i in range(10))

=)

Steve

Andrey Tatarinov
Guest
Posts: n/a

 01-13-2005
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) if i%2==0]
>>> what if I want i to be "i-2" if i%2 is not equal to 0?
>>>

>>
>> Hmm:
>>
>> 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 is to avoid polluting the local namespace
> with "newval". I further presume you also have plans to do something

no, the point is in grouping definition of newval() with place where it
is used.

Nick Coghlan
Guest
Posts: n/a

 01-13-2005
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

>
> no, the point is in grouping definition of newval() with place where it
> is used.

I'd have said the point was both

But yeah, unfortunately the 'leaking list comp' problem won't be fixed in the
2.x series due to the compatibility problem. Fortunately, generator expressions
didn't inherit the issue.

Cheers,
Nick.

--
Nick Coghlan | (E-Mail Removed) | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net