[Tutor] Question about Dictionaries

Dave Angel davea at ieee.org
Tue Aug 17 01:02:47 CEST 2010


Chorn, Guillaume wrote:
> Hi All,
>
> I know that I can look up the value for a particular key in a
> dictionary, but can I look up the key associated with a particular
> value?  I understand that this could be problematic from the standpoint
> of multiple keys having the same value, but even then I feel like Python
> could just return a list of keys with that value.
>
> Thanks!
>
> Guillaume  
>
> Notice:  This e-mail message, together with any attachments, contains
> information of Merck & Co., Inc. (One Merck Drive, Whitehouse Station,
> New Jersey, USA 08889), and/or its affiliates Direct contact information
> for affiliates is available at 
> http://www.merck.com/contact/contacts.html) that may be confidential,
> proprietary copyrighted and/or legally privileged. It is intended solely
> for the use of the individual or entity named on this message. If you are
> not the intended recipient, and have received this message in error,
> please notify us immediately by reply e-mail and then delete it from 
> your system.
>
>   
As long as you don't mind it being (relatively) slow, you could do 
something like (untested):

[key for key, item in mydict if item == value]

DaveA




More information about the Tutor mailing list