Howto find dict members from a list of keys

James Stroud jstroud at mbi.ucla.edu
Thu Mar 8 06:42:41 EST 2007


Gabriel Genellina wrote:
> En Thu, 08 Mar 2007 05:37:48 -0300, Steven D'Aprano 
> <steve at REMOVEME.cybersource.com.au> escribió:
> 
>> On Thu, 08 Mar 2007 05:26:22 -0300, Gabriel Genellina wrote:
>>
>>> found_dic_members = [d[key] for key in l]
>>
>> *self stares at the line of code*
>>
>> *self thinks about what he just posted*
>>
>> *self realises with acute embarrassment that he's jumped to conclusions
>> and completely misunderstood the Original Poster's question*
>>
>> Oops! I'll just be slinking away now...
> 
> LOL! :)
> 
> (...now! But when I saw your previous post, I had to check whether it 
> was *me* who misunderstood the OP answering with a silly one-liner...)
> 
> 
> --Gabriel Genellina
> 
I think it was a silly one-liner.

If you look closely at the example, the only mapping of

[7, 8] ==> [8, 9]

is

[key, key] ==> [value, value]

Look again at the example and consider the positional relationships.

The answer, then, is trivial if not silly:

    [d[k] for k in l]

It doesn't appear he as asking for the interesting case:

[value, value] ==> [key, key]

Its easy to see how Gabriel was thrown for a loop on this one.

James



More information about the Python-list mailing list