Python check if a given key exists in a dictionary

Posted by Samath
  394
January 13, 2017

 

This program basically check if a given key exists in a dictionary. This is written using the python programming language. 

d = {'k1': 'v1', 'k2': 'v2', 'k3': 'v3'}


print 'd contains k1? ', d.has_key('k1')

print 'd contains k4? ', d.has_key('k4')

print 'k2 in d? ', 'k2' in d