Dictionary .get method usage in python

Description

The 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.

Return Value

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

Example

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

#!/usr/bin/python

diction = {'Name': 'Kumar', 'Age': 12}

print "Value : %s" %  diction.get('Age')
print "Value : %s" %  diction.get('School', "Nothing")

When we run above program, it produces following result −

Value : 12
Value : Nothing
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s