[Tutor] List comprehension for dicts?
Pete
pkoek11 at xs4all.nl
Fri Aug 20 01:51:08 CEST 2010
On 2010-08-19, at 5:25 PM, Emile van Sebille wrote:
> On 8/19/2010 7:51 AM Pete said...
>> Hi,
>>
>> I've been reading up on list comprehensions lately, all userful and powerful stuff - trying to wrap my brain around it :)
>>
>> As the examples all seem to relate to lists, I was wondering if there is an elegant similar way to apply a function to all keys in a dictionary?
>>
>> (without looping over it, of course)
>
> Just fyi, as it sounds you may not understand. List Comprehensions loop over the list. There's no other magic going on that avoids that.
>
> result = [ ii for ii in lst if cond ]
>
> is just shorthand for
>
> result = []
> for ii in lst:
> if cond:
> result.append(ii)
>
> Emile
Ah, so list comprehensions are a purely syntactic construct?
I had the feeling there might be a performance benefit of some kind.
thanks for all the contributions on this topic, it's been greatly helpful to my understanding.
-Pete
More information about the Tutor
mailing list