Dictionary get() Method



Description

Python dictionary method get() returns a value for the given key. If key is not available then returns default value None.

Syntax

Following is the syntax for get() method −

dict.get(key, default = None)

Parameters

  • key − This is the Key to be searched in the dictionary.

  • default − This is the Value to be returned in case key does not exist.

NA

Return Value

This method returns a value for the given key. If key is not available, then returns default value None.

Example

The following example shows the usage of fromkeys() method.

dict = {'Name': 'Zabra', 'Age': 7}
print "Value : %s" % dict.get('Age')
print "Value : %s" % dict.get('Education', "Never")

When we run above program, it produces following result −

Value : 7
Value : Never
python_dictionary_methods.htm
Advertisements