[Tutor] Question about Dictionaries
Alan Gauld
alan.gauld at btinternet.com
Tue Aug 17 01:20:43 CEST 2010
"Chorn, Guillaume" <guillaume_chorn at merck.com> wrote
> dictionary, but can I look up the key associated with a particular
> value?
Not directly but its not hard to do bearing in mind you will
get a collection back.:
>>> d = {1:10,2:20,3:10,4:20}
>>> val = 10
>>> ks = [k for k in d if d[k] == val]
>>> ks
[1, 3]
>>>
You need to trawl the dictionary whatever you do
(unless you get very sophisticated and build a double
dictionary class, but then insertions get slow because
you have to update both versions...)
HTH,
--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/
More information about the Tutor
mailing list