Python dict get()

In the article we will discuss how to use the dict.get() function with some examples. Then we will also discuss the differences between dict[key] and dict.get(key).

dict.get() Syntax:

In Python dict class provides a member function get() to get the value associated with a key,

dict.get(key[, default_value])

Parameters:

It accepts two parameters,

  • Key:
    • The key, that needs to be searched in the dictionary.
  • default_value:
    • The default value, which will be returned if the dictionary does not contain the given key.
    • It is an optional parameter and if not provided then None will be used instead of it.

Return:

  • If the given key exists in the dictionary then it returns the value associated with the key.
  • If the given key does not exist in the dictionary and default_value is provided then returns the default_value.
  • If the given key does not exist in the dictionary and default_value is not provided then returns the None.

So, basically get() function accepts a key as an argument and returns the value associated with that key in the dictionary. If the key does not exist in the dictionary then it either returns the default_value if provided, otherwise returns None.

Let’s look at some examples of dict.get()

dict.get() Examples

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

Suppose we have a dictionary, which contains some strings as keys and each key has an integer value associated with it. We want to fetch the value associated with the key ‘at’. Let’s see how to do that using get() function,

# Dictionary with strings as keys
# and ints as values
word_freq = {
    "Hello": 56,
    "at": 23,
    "test": 43,
    "this": 78
}

# Get the value associated with a key 'at'
value = word_freq.get('at')

# Value of the key 'at'
print('Value of key "at" : ', value)

Output:

Value of key "at" :  23

We passed the ‘at’ as an argument to the get() function. Which returned the value of the given key ‘at’.

But what of the given key does not exist in the dictionary? Let’s look at another example of this,

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

If we pass an unknown key in the get() function as argument i.e. a key that does not exist in the dictionary, the get() function will return the default value i.e.

# Dictionary with strings as keys
# and ints as values
word_freq = {
    "Hello": 56,
    "at": 23,
    "test": 43,
    "this": 78
}

# Get the value associated with a key 'why'
value = word_freq.get('Why')

# Value of the key 'Why'
print('Value of key "Why" : ', value)

Output:

Value of key "Why" :  None

Here we passed a key ‘why’ as an argument to the get() function. As this key does not exist in the dictionary. So get() function decided to return the default value. But we didn’t provide the default value too, therefore get() function returned the None.

What if we pass the default value too as an argument?

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

Here we will pass two arguments in the get() function,

  • An unknown key i.e. a key that doesn’t exist in the dictionary.
  • A default value
# Dictionary with strings as keys
# and ints as values
word_freq = {
    "Hello": 56,
    "at": 23,
    "test": 43,
    "this": 78
}

# Get the value associated with a key 'why'
value = word_freq.get('Why', 0)

# Value of the key 'Why'
print('Value of key "Why" : ', value)

Output:

Value of key "Why" :  0

As the given key does not exist in the dictionary so get() function will return the default value which we provided i.e. 0.

Dict.get(key) vs dict[key]

In a dictionary in python, we can get the value of a key by subscript operator too, then why do we need a separate get() function to fetch the value of a key?

To understand the answer to this question, let’s start with an example,

Get the value of a key in a dictionary using [] i.e. the subscript operator

# Dictionary with strings as keys
# and ints as values
word_freq = {
    "Hello": 56,
    "at": 23,
    "test": 43,
    "this": 78
}
# Get the value of a key in a dictionary using [] i.e. the subscript operator
value = word_freq['at']

# Value of the key 'Why'
print('Value of key "at" : ', value)

Output:

Value of key "at" :  23

In the above example we fetched the value of key ‘at’ from the dictionary using []. But what if the key does not exist in the dictionary? In that case [] operator will return KeyError.

Check out this example,

# Dictionary with strings as keys
# and ints as values
word_freq = {
    "Hello": 56,
    "at": 23,
    "test": 43,
    "this": 78
}
# Get the value of a key in a dictionary using [] i.e. the subscript operator
value = word_freq['Why']

# Value of the key 'Why'
print('Value of key "at" : ', value)

Error

KeyError: 'Why'

As the dictionary does have any key ‘Why’, therefore when we tried to access its value sing [] operator then it raised an error. Whereas, if we use the get() function to access the value of a key that does not exist in the dictionary then it will not throw any error, instead it will return a default value. For example,

# Dictionary with strings as keys
# and ints as values
word_freq = {
    "Hello": 56,
    "at": 23,
    "test": 43,
    "this": 78
}

# Get the value of a key in a dictionary using get() method
value = word_freq.get('Why')

# Value of the key 'Why'
print('Value of key "Why" : ', value)

Output:

Value of key "Why" :  None

So, the main difference between [] and get() function is that, if the given key does not exist in the dictionary the get() function will return the default value, whereas, [] will raise the KeyError.

This is how we can use get() method of dictionary class to fetch value in dictionary.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top