[Python-ideas] Testing Key-Value Membership In Dictionaries

Sven Marnach sven at marnach.net
Sun Oct 9 23:03:02 CEST 2011


Karthick Sankarachary schrieb am So, 09. Okt 2011, um 13:25:16 -0700:
>  >>> tel = {'jack': 4098, 'sape': 4139}
> 
> 
>  >>> ('jack', 4098) in tel
>  True
>  >>> ('jack', 4000) in tel
>  False
>  >>> 'jack' in tel
>  True

These semantics are inconsistent:

>>> d = {"a": 1, ("a", 2): 3}
>>> ("a", 2) in d

What result do you expect now?  False, because the value for "a" is 1,
or True, because ("a", 2) occurs as a key?  Neither answer seems to be
useful.

Moreover, the functionality you want is easily available as something like

    tel.get("jack") == "4098"

Cheers,
    Sven



More information about the Python-ideas mailing list