Python dictionary get() Method


Contents

Description

Python dictionary way get() revisits a worth for the known key. If enter is not obtainable then returns defaulting worth None. Get() technique for dictionaries in Python. In python dictionaries, subsequent is a conformist process to admission a value for a key. ... This way returns the worth for the agreed key, if present in the dictionary. If not, then it would revisit None (if get() is utilized with only one argument).

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

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

Here at Intellinuts, we have created a complete Python tutorial for Beginners to get started in Python.



Navigation
Tutorial
Tutorial
Tutorial