List Comprehension Syntax

Reinhold Birkenfeld reinhold-birkenfeld-nospam at wolke7.net
Sun Jul 11 02:47:55 EDT 2004


Mitja wrote:
> Moosebumps <crap at crap.crap>
> (news:8OXHc.13877$4y7.5713 at newssvr27.news.prodigy.com) wrote:
>>> I usually do
>>> result = [
>>>     element.replace( 'blah', 'moof' )
>>>     for element in list
>>>     if element[0:4] == 'blah'
>>> ]
>>> It seems clean and logical enough to me - like e.g.
>>> defining big dicts.
>>>
>>>
>>
>> But what if you have multiple for and if's?  Seems like
>> you would want to indent them then.
> 
> Hm... I never used a complicated enough case :)
> I still think I wouldn't indent them - it is clear enough how they corelate.
> If anything, I'd do
> foo = [
>   a*b
>   for a in range(10)
>     for b in range(10)
>       for c in ['foo','bar']
>   if 42==True
> ]
> 
> Can you have multiple ifs at all?

No, but you can have if (...) and (...) or (...)

Reinhold

-- 
Wenn eine Linuxdistribution so wenig brauchbare Software wie Windows
mitbrächte, wäre das bedauerlich.  Was bei Windows der Umfang eines
"kompletten Betriebssystems" ist, nennt man bei Linux eine Rescuedisk.
  -- David Kastrup in de.comp.os.unix.linux.misc



More information about the Python-list mailing list