Python Dictionary Get() Method

Python Dictionary Get Value for a Key

Python dict.get() method returns the value corresponding to the specified key.

This tutorial introduces you to get() method of Dictionary class in Python, and its usage, with the help of example programs.

Syntax – dict.get()

The syntax of dictionary get() method is

dictionary.get(key, value)

where

ParameterDescription
key[mandator] The key for which value has to be fetched from the dictionary.
value[optional] If specified key does not exist, get() returns this value.

Return Value

  • dict.get() returns the value corresponding to the specified key, if present.
  • If the key is not present, and value (second argument) is given, then get() returns this value.
  • If the key is not present, and value (second argument) is not given, then get() returns None.

Example 1: Dictionary get()

In this example, we will create a dictionary with some key:value pairs, and use get() method to access values corresponding to the specific keys we provide as arguments.

Python Program

myDict = {
	'foo':12,
	'bar':14
}

print(myDict.get('bar'))
Run

Output

14

‘bar’ key is present in the dictionary. Therefore, get() method returned the value corresponding to that key.

Example 2: Dictionary get() – Key not Present

In this example, we will create a dictionary with some key:value pairs, and use get() method to access value corresponding to a specific key that is not present in the dictionary.

Python Program

myDict = {
	'foo':12,
	'bar':14
}

#key not present in dictionary
print(myDict.get('moo'))
Run

Output

None

The key ‘moo’ is not present in the dictionary. Also, we have not given any second argument to get() method for default value. In these kind of scenarios, as we already see in Return Value section, Dictionary.get() method returns value None of type NoneType.

Example 3: Dictionary get() – With Default Value

You can also tell get() method to return a default value instead of None, if key-value pair is not present for the key specified. Provide the default value as second argument to get() method.

Python Program

myDict = {
	'foo':12,
	'bar':14
}

print(myDict.get('moo', 10))
Run

Output

10

dict.get() vs Accessing dict using index

Most of the times, you would see or use indexing style of accessing values of a dictionary with key as index. A sample code snippet is

value1 = myDictionary[key1]

There is a downside of using this style. Which is, when there is no key:value pair for the key we have mentioned, you would get KeyError.

Following is a example demonstrating how using square brackets to get a value corresponding to given key in dictionary ends up with KeyValue error.

Python Program

myDict = {
	'foo':12,
	'bar':14
}

print(myDict['moo'])
Run

Output

Traceback (most recent call last):
  File "example.py", line 6, in <module>
    print(myDict['moo'])
KeyError: 'moo'

So, you may have to explicitly check if the key is present, and then access the dictionary using key as index.

Summary

In this tutorial of Python Examples, we learned how to use Dictionary get() method to access values, with help of well detailed Python programs.