Python USES the dict dictionary to determine whether a specified key value exists

  • 2020-04-02 14:43:26
  • OfStack

This example shows how python determines the existence of a specified key value by dict. Share with you for your reference. The details are as follows:

There are two ways to determine the existence of a specified key value in python, one by the method has_key for the dictionary object, and the other by the in method. Here is a detailed example.


d={'site':'//www.jb51.net','name':'jb51','is_good':'yes'}
# methods 1 Through: has_key
print d.has_key('site')
# methods 2 Through: in
print 'body' in d.keys()

I hope this article has helped you with your Python programming.


Related articles: