[Tutor] Re: How to get a key from dictionary?

Matthew Sherborne miracle@paradise.net.nz
Wed, 27 Mar 2002 08:28:38 +1200


This is a multi-part message in MIME format.

------=_NextPart_000_002A_01C1D569.652C29C0
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Sorry to be a show off, but I reckon that this way may be faster. Sorry =
again. The other replies are very helpful.  :)

def findKey(val):
  for key, value in dict.items():
    if value =3D=3D val: return key
  return None
 =20
GBU
Matthew Sherborne

----- Original Message -----=20
From: "Don Arnold" <darnold02@sprynet.com>
To: <printers@sendme.cz>; <python-list@python.org>; <tutor@python.org>; =
<activepython@listserv.activestate.com>; <python-help@python.org>
Sent: Tuesday, March 26, 2002 11:51 AM
Subject: Re: How to get a key from dictionary?


> >>> targetval =3D 2
> >>> dict =3D {'aa':1, 'bb':2, 'cc': 3, 'dd': 1}
> >>> targetval =3D 2
> >>> for key in dict.keys():
>           if dict[key] =3D=3D targetval:
>             print "found", targetval, "at key", key
>=20
> found 2 at key bb
> >>> targetval =3D 1
> >>> for key in dict.keys():
>           if dict[key] =3D=3D targetval:
>             print "found", targetval, "at key", key
>=20
> found 1 at key aa
> found 1 at key dd
>=20
> ----- Original Message -----
> From: "A" <printers@sendme.cz>
> To: <python-list@python.org>; <tutor@python.org>;
> <activepython@listserv.activestate.com>; <python-help@python.org>
> Sent: Monday, March 25, 2002 4:16 PM
> Subject: How to get a key from dictionary?
>=20
>=20
> > Hi,
> > Is there a possibility to get, from a dictionary, a key according to =
a
> > value ?
> > For example
> > I have a dictionary
> >
> > dict=3D{'aa':1,'bb':2}
> >
> > and
> > dict['aa']
> > is 1
> >
> > But how can I for value 1 find out  key? (That is here  'aa')
> >
> > Thank you for help
> > Ladislav
> >
> >
> > _______________________________________________
> > ActivePython mailing list
> > ActivePython@listserv.ActiveState.com
> > To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
>=20
> _______________________________________________
> ActivePython mailing list
> ActivePython@listserv.ActiveState.com
> To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
>=20

------=_NextPart_000_002A_01C1D569.652C29C0
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2712.300" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY>
<DIV><FONT face=3DArial size=3D2>Sorry to be a show off, but I reckon =
that this way=20
may be faster. Sorry again. The other replies are very helpful.&nbsp;=20
:)</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT><FONT face=3DArial =
size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DCourier size=3D2>def findKey(val):</FONT></DIV>
<DIV><FONT face=3DCourier size=3D2>&nbsp; for key, value in=20
dict.items():</FONT></DIV>
<DIV><FONT face=3DCourier size=3D2>&nbsp;&nbsp;&nbsp; if value =3D=3D =
val: return=20
key</FONT></DIV>
<DIV><FONT face=3DCourier size=3D2>&nbsp; return None</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp; </FONT></DIV>
<DIV><FONT face=3DArial size=3D2>GBU</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>Matthew Sherborne</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>----- Original Message ----- =
</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>From: "Don Arnold" &lt;</FONT><A=20
href=3D"mailto:darnold02@sprynet.com"><FONT face=3DArial=20
size=3D2>darnold02@sprynet.com</FONT></A><FONT face=3DArial =
size=3D2>&gt;</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>To: &lt;</FONT><A=20
href=3D"mailto:printers@sendme.cz"><FONT face=3DArial=20
size=3D2>printers@sendme.cz</FONT></A><FONT face=3DArial size=3D2>&gt;; =
&lt;</FONT><A=20
href=3D"mailto:python-list@python.org"><FONT face=3DArial=20
size=3D2>python-list@python.org</FONT></A><FONT face=3DArial =
size=3D2>&gt;;=20
&lt;</FONT><A href=3D"mailto:tutor@python.org"><FONT face=3DArial=20
size=3D2>tutor@python.org</FONT></A><FONT face=3DArial size=3D2>&gt;; =
&lt;</FONT><A=20
href=3D"mailto:activepython@listserv.activestate.com"><FONT face=3DArial =

size=3D2>activepython@listserv.activestate.com</FONT></A><FONT =
face=3DArial=20
size=3D2>&gt;; &lt;</FONT><A =
href=3D"mailto:python-help@python.org"><FONT face=3DArial=20
size=3D2>python-help@python.org</FONT></A><FONT face=3DArial=20
size=3D2>&gt;</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>Sent: Tuesday, March 26, 2002 11:51 =
AM</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>Subject: Re: How to get a key from=20
dictionary?</FONT></DIV>
<DIV><FONT face=3DArial><BR><FONT size=3D2></FONT></FONT></DIV><FONT =
face=3DArial=20
size=3D2>&gt; &gt;&gt;&gt; targetval =3D 2<BR>&gt; &gt;&gt;&gt; dict =3D =
{'aa':1,=20
'bb':2, 'cc': 3, 'dd': 1}<BR>&gt; &gt;&gt;&gt; targetval =3D 2<BR>&gt;=20
&gt;&gt;&gt; for key in dict.keys():<BR>&gt;=20
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if dict[key] =
=3D=3D=20
targetval:<BR>&gt;=20
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print =

"found", targetval, "at key", key<BR>&gt; <BR>&gt; found 2 at key =
bb<BR>&gt;=20
&gt;&gt;&gt; targetval =3D 1<BR>&gt; &gt;&gt;&gt; for key in =
dict.keys():<BR>&gt;=20
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if dict[key] =
=3D=3D=20
targetval:<BR>&gt;=20
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print =

"found", targetval, "at key", key<BR>&gt; <BR>&gt; found 1 at key =
aa<BR>&gt;=20
found 1 at key dd<BR>&gt; <BR>&gt; ----- Original Message -----<BR>&gt; =
From:=20
"A" &lt;</FONT><A href=3D"mailto:printers@sendme.cz"><FONT face=3DArial=20
size=3D2>printers@sendme.cz</FONT></A><FONT face=3DArial =
size=3D2>&gt;<BR>&gt; To:=20
&lt;</FONT><A href=3D"mailto:python-list@python.org"><FONT face=3DArial=20
size=3D2>python-list@python.org</FONT></A><FONT face=3DArial =
size=3D2>&gt;;=20
&lt;</FONT><A href=3D"mailto:tutor@python.org"><FONT face=3DArial=20
size=3D2>tutor@python.org</FONT></A><FONT face=3DArial =
size=3D2>&gt;;<BR>&gt;=20
&lt;</FONT><A =
href=3D"mailto:activepython@listserv.activestate.com"><FONT=20
face=3DArial =
size=3D2>activepython@listserv.activestate.com</FONT></A><FONT=20
face=3DArial size=3D2>&gt;; &lt;</FONT><A =
href=3D"mailto:python-help@python.org"><FONT=20
face=3DArial size=3D2>python-help@python.org</FONT></A><FONT =
face=3DArial=20
size=3D2>&gt;<BR>&gt; Sent: Monday, March 25, 2002 4:16 PM<BR>&gt; =
Subject: How to=20
get a key from dictionary?<BR>&gt; <BR>&gt; <BR>&gt; &gt; Hi,<BR>&gt; =
&gt; Is=20
there a possibility to get, from a dictionary, a key according to =
a<BR>&gt; &gt;=20
value ?<BR>&gt; &gt; For example<BR>&gt; &gt; I have a =
dictionary<BR>&gt;=20
&gt;<BR>&gt; &gt; dict=3D{'aa':1,'bb':2}<BR>&gt; &gt;<BR>&gt; &gt; =
and<BR>&gt;=20
&gt; dict['aa']<BR>&gt; &gt; is 1<BR>&gt; &gt;<BR>&gt; &gt; But how can =
I for=20
value 1 find out&nbsp; key? (That is here&nbsp; 'aa')<BR>&gt; =
&gt;<BR>&gt; &gt;=20
Thank you for help<BR>&gt; &gt; Ladislav<BR>&gt; &gt;<BR>&gt; =
&gt;<BR>&gt; &gt;=20
_______________________________________________<BR>&gt; &gt; =
ActivePython=20
mailing list<BR>&gt; &gt; </FONT><A=20
href=3D"mailto:ActivePython@listserv.ActiveState.com"><FONT face=3DArial =

size=3D2>ActivePython@listserv.ActiveState.com</FONT></A><BR><FONT =
face=3DArial=20
size=3D2>&gt; &gt; To unsubscribe: </FONT><A=20
href=3D"http://listserv.ActiveState.com/mailman/mysubs"><FONT =
face=3DArial=20
size=3D2>http://listserv.ActiveState.com/mailman/mysubs</FONT></A><BR><FO=
NT=20
face=3DArial size=3D2>&gt; <BR>&gt;=20
_______________________________________________<BR>&gt; ActivePython =
mailing=20
list<BR>&gt; </FONT><A =
href=3D"mailto:ActivePython@listserv.ActiveState.com"><FONT=20
face=3DArial =
size=3D2>ActivePython@listserv.ActiveState.com</FONT></A><BR><FONT=20
face=3DArial size=3D2>&gt; To unsubscribe: </FONT><A=20
href=3D"http://listserv.ActiveState.com/mailman/mysubs"><FONT =
face=3DArial=20
size=3D2>http://listserv.ActiveState.com/mailman/mysubs</FONT></A><BR><FO=
NT=20
face=3DArial size=3D2>&gt; </FONT></BODY></HTML>

------=_NextPart_000_002A_01C1D569.652C29C0--