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

Terry Reedy tjreedy at udel.edu
Mon Oct 10 00:29:39 CEST 2011

> Currently, to check whether a single key is in a dictionary, we use the
> "in" keyword. However, there is no built-in support for checking if a
> key-value pair belongs in a dictionary.

Python is developed according to practical need rather than theoretical 
consistency or symmetry. In Python 3, 'key in dict' is the same as 'key 
in dict.keys()'. Similarly, 'for item in dict' is the same as 'for item 
in dict.keys()'. One can look for or iterate over key-value items by 
replacing 'keys' with 'items' in either construction above. Dict keys 
get the special treatment of being the default because they are the most 
commonly used for search and iteration.

Terry Jan Reedy

More information about the Python-ideas mailing list