Determine whether a key is present in a dictionary

Viewed 119432

Possible Duplicate:
'has_key()' or 'in'?

I have a Python dictionary like :

mydict = {'name':'abc','city':'xyz','country','def'}

I want to check if a key is in dictionary or not. I am eager to know that which is more preferable from the following two cases and why?

1> if mydict.has_key('name'):
2> if 'name' in mydict:
4 Answers
Related