List Comprehension Syntax
Mitja
nun at example.com
Sat Jul 10 17:00:45 EDT 2004
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?
> I think I might just get used to the multiline syntax
> since LCs are very useful... it just seems awkward for
> some reason.
>
> result = [ x for x in blah if f(x) ]
>
> seems more elegant than:
>
> result = [
> x
> for x in blah
> if f(x) ]
>
> but I like to keep things consistent and have
> maintainability, so then the second one wins. i.e. the
> second one "scales better" to more complicated
> expressions!
>
> MB
More information about the Python-list
mailing list