You can do the same check using a default argument in dict.get such as...
tel.get('jack', None) == 4098 True tel.get('jack', None) == 4000 False tel.get('jill', None) == 4000 False
Hello Python Ideas,
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.
Currently, we presuppose that the object being checked has the same type as that of the key. What if we allowed the "in" operator to accept a tuple
denotes a (mapped) key-value pair?
Let us consider how that might work using the canonical example given in
- John On Sun, Oct 9, 2011 at 4:25 PM, Karthick Sankarachary < karthick.sankarachary@gmail.com> wrote: that the
tutorial:
tel = {'jack': 4098, 'sape': 4139}
('jack', 4098) in tel True ('jack', 4000) in tel False 'jack' in tel True
As you can see, the "in" operator would interpret the object as either a key or a key-value pair depending on the actual types of the object, key and value. In the key itself happens to be a tuple, then the key-value membership test would involve a nested tuple, whose first item is a tuple denoting the key.
Best Regards, Karthick Sankarachary
_______________________________________________ Python-ideas mailing list Python-ideas@python.org http://mail.python.org/mailman/listinfo/python-ideas