How to test if a key in a dictionary exists?

Frank supervau at gmail.com
Sat Mar 10 16:17:11 EST 2007


Hi,

does anyone know how one can test if, e.g., a dictionary 'name' has a
key called 'name_key'?

This would be possible:

keys_of_names = names.keys()
L = len(keys_of_names)

for i in range(L):
if keys_of_names[i] == name_key:
print 'found'


But certainly not efficient. I would expect there is something like:

name.is_key(name_key)

I appreciate your help!

Frank




More information about the Python-list mailing list