Latest web development tutorials

Python dictionary (Dictionary) get () method

description

Python dictionary (Dictionary) get () function returns the value of the specified key, if the value is not in the dictionary or a default value.

grammar

get () method syntax:

dict.get(key, default=None)

parameter

  • key - the dictionary to find the key.
  • default - if the specified key does not exist, return the default value.

return value

Returns the value of the specified key, if the value is not in the dictionary or a default value None.

Examples

The following example shows the get () method using the function:

#!/usr/bin/python

dict = {'Name': 'Zara', 'Age': 27}

print "Value : %s" %  dict.get('Age')
print "Value : %s" %  dict.get('Sex', "Never")

The above example output is:

Value : 27
Value : Never