[Tutor] List comprehension for dicts?

Emile van Sebille emile at fenx.com
Thu Aug 19 23:25:34 CEST 2010


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




More information about the Tutor mailing list