Skip to content
Related Articles
Open in App
Not now

Related Articles

Python Dictionary get() Method

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 25 Aug, 2022
Improve Article
Save Article

Python Dictionary get() Method return the value for the given key if present in the dictionary. If not, then it will return None (if get() is used with only one argument).

Python Dictionary get() Method Syntax:

Syntax : Dict.get(key, default=None)


  • key: The key name of the item you want to return the value from
  • Value: (Optional) Value to be returned if the key is not found. The default value is None.

Returns: Returns the value of the item with the specified key or the default value.

Python Dictionary get() Method Example:


d = {'coding': 'good', 'thinking': 'better'}



Example 1: Python get() Method with default parameter.


d = {1: '001', 2: '010', 3: '011'}
# since 4 is not in keys, it'll print "Not found"
print(d.get(4, "Not found"))


Not found

Example 2: Python Dictionary get() method chained

The get() to check and assign in absence of value to achieve this particular task. Just returns an empty Python dict() if any key is not present.


test_dict = {'Gfg' : {'is' : 'best'}}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# using nested get()
# Safe access nested dictionary key
res = test_dict.get('Gfg', {}).get('is')
# printing result
print("The nested safely accessed value is :  " + str(res))


The original dictionary is : {'Gfg': {'is': 'best'}}
The nested safely accessed value is :  best

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!