Python Tutorial Python Advanced Python References Python Libraries

Python Dictionary - get() Method



The Python get() method is used to find out the value of specified key in the dictionary.

Syntax

dictionary.get(key, val)

Parameters

key Required. key which need to be searched in the dictionary.
val Optional value returned when specified key is not found in dictionary. default value is None.

Return Value

Returns the value of the specified key in the dictionary.

Example:

In the example below, the get() method is used to find out the value of the specified key in the dictionary.

Info = {
  "name": "John",
  "age": 25,
  "city": "London"
}
print(Info.get("name"))

print(Info.get("hobby"))

print(Info.get("hobby", "key is missing in the dictionary."))

The output of the above code will be:

John
None
key is missing in the dictionary.

❮ Python Dictionary Methods