Python Dictionary get() Method Tutorial

In this section we will learn what the Dictionary get() method is and how to use it in Python.

What is Dictionary get() Method in Python?

The Python Dictionary get() method is used to get the value of a key in a dictionary.

Python Dictionary get() Method Syntax:

dictionary.get(key,defaultValue)

Dictionary get() Method Parameter:

The method takes two arguments:

  • The first argument is the key we want to take its value in a dictionary.
  • The second argument is a default value that will be returned if the specified key didn’t exist in the target dictionary. The default value is set to None.

Dictionary get() Method Return Value

The return value of this method is either the value of the specified key (if it exists in the dictionary) or the default value we’ve set as the second argument of this method (if any) in case the key wasn’t in the target dictionary.

Also, the value None could return if the specified key wasn’t in the dictionary and we didn’t set any default value as the second argument of this method.

Example: using python dictionary get() method

dictionary = {

"name":"Jack",

"lastName":"Doe"

}

result = dictionary.get("age","There's no age key in this dictionary")

print(result)

Output:

There's no age key in this dictionary
Facebook
Twitter
Pinterest
LinkedIn

Top Technologies