Python: Dictionary get() function tutorial and examples

Dictionary get() function tutorial with examples in Python.

In this article, we are going to see how we can use the dict.get( ) function with some examples.

Syntax : dict.get(key, defaultValue)

 Where,

  1. Key : The key that is to be searched inside the dictionary.
  2. defaultValue : The default value to be returned in case we don’t find the key inside the dictionary.

The function returns the value associated with the key in case the key is found in the dictionary. Otherwise it returns the default value that we provides to it, if there was no default value provided it returns none.

Get value by key in a dictionary using dict.get() :

Let us take an example where there are 4 elements inside a dictionary. We will try to find the value associated with “B”.

#Program :

dictWords = {
    "A": 65,
    "B": 32,
    "V": 34,
    "U": 87 }
#Fucntion returns the value associated with the key ‘'B'’
dictValue = dictWords.get('B')
#Value associateed with the key 'B'
print('Value of the key "B" : ', dictValue)
Output : 
Value of the key "B" : 32

Get the value of a key that does not exist in a dictionary :

 In the previous example we were looking for the value associated with an existing key, here we will try to see what happens when the key doesn’t exist in the dictionary.

#Program :

dictWords = {
    "A": 56,
    "B": 23,
    "V": 43,
    "U": 78
}
#Fucntion returns the value associated with the non- existent key 'D'
dictValue = dictWords.get('D')
#Value associateed with the key 'D'
print('Value of the non-existent key "D" : ', dictValue)
Output :
Value of the non-existent key "D" : None

When the key “D” which was absent was passed into the function, the compiler prints none, which is the default value (None is the default value as we have not specified any other value in the place of default value).

Get the default value for the key that does not exist in a dictionary :

 Here we will again check for the same non-existent key, however this time we will provide the function with a default value.

#Program :

dictWords = {
    "A": 56,
    "B": 23,
    "V": 43,
    "U": 78
}
#Fucntion returns the value associated with the non- existent key 'D' i.e. default value
dictValue = dictWords.get('D',"NA")
#Value associateed with the key 'D'
print('Value of the non-existent key "D" : ', dictValue)
Output :
Value of the non-existent key "D" : NA

It returns the default value that we provided when the key we were searching for was non-existent.

Dict.get(key) vs dict[key]

 We can also use the [ ] to find out the value associated to a key inside a dictionary.

So, let’s see the implementation of it.

#Program :

dictWords = {
    "A": 56,
    "B": 23,
    "V": 43,
    "U": 78
}
#Fucntion returns the value associated with the key 'B' using []
dictValue = dictWords['B']
#Value associateed with the key 'B'
print('Value of the key "B" : ', dictValue)
Output :
Value of the key "B" : 23

It does the same thing when we check for an existent key. Let’s try with the non-existent one.

Output :
KeyError: 'D'

So the only difference in these two methods is that while looking for a non-existent key using the .get( ) function we get none as output or a default value that we assigned. However while using [ ] we get a KeyError in the program.