[Tutor] using "in" with a dictionary

Wayne Werner waynejwerner at gmail.com
Wed Sep 29 00:10:52 CEST 2010


On Tue, Sep 28, 2010 at 4:58 PM, Alex Hall <mehgcap at gmail.com> wrote:

> Hi all, yet again:
> I have a dictionary that will look something like:
> d={
>  (1,2):"a",
>  (3,4):"b"
> }
>
> How can I say:
> if (1,2) in d: print d[(1,2)]
> This is false, so I expect to have to use d.keys, but I am not quite sure
> how.
> I will be using this in a loop, and I have to know if there is a key
> in the dictionary called (i,j) and, if there is, I have to grab the
> value at that slot. If not I have to print something else. When I
> tried "in" in the interpreter, I got something about builtin function
> not being iterable. TIA for any suggestions.
>

 >>> d = {(1,2):"a"}
>>> if (1,2) in d:
...     print d[(1,2)]
...
a

you tried that?

-Wayne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100928/d0311024/attachment.html>


More information about the Tutor mailing list