Python Dictionary get() Method

Python dictionary get() method is used to get the value of the element with the specified keys from Dictionary. This method takes optional value param which is used to return the default value when a key is not present.

If the key is present in the dictionary, get() will return the value associated with that key. However, if the key is not present in the dictionary and the value param is specified then the method returns the specified value. On the other hand, if the key is not present in the dictionary and the value is not specified then the Python dictionary get() returns None value.

All dictionary methods are available on the dictionary methods page.

Quick examples Python Dictionary get() method

Following are quick examples of how to use the Dictionary get() method.


course={'language': 'python', 'fee': 4000}
# Example 1: Using get() method to get the value from dictionary
print('language:', course.get('language'))
print('fee:', course.get('fee'))

# Example 2: Using get() to get the value as a None
print('duration:', course.get('duration'))

# Example 3: Using get() to get the value as specified
print('duration:', course.get('duration','Not in dictionary'))

1. Syntax of the get() method

Following is the syntax of the Python Dictionary get() method.


# Syntax of the get()
dict.get(key,value)

1.1 Parameter of the get() method

  • key: (Required) Key to be searched in the dictionary.
  • value: (Optional) Value to be returned if the key is not present in the dictionary.

1.2 Return value from get() method

  • Returns value of the specified key if a key is present in the dictionary.
  • Returns None, if the key is not present in the dictionary and value param is not specified.
  • Returns param value, if a key is not present in the dictionary and the value specified.

2. Usage of Python Dictionary get() Method

2.1 Return Dictionary Value Example

The value will be returned if a key is present in the dictionary. For instance,


# Using get() method to get the value from dictionary
course={'language': 'python', 'fee': 4000}

print('language:', course.get('language'))
# language: python

print('fee:',course.get('fee'))
# fee: 4000

This behavior is similar to the Python Dictionary setdefault() method. The setdefault() method also returns a value for the given key.

2.2 Return None Value Example

None will return if a key is not present in the dictionary, and the value parameter is not specified. For example,


# Using get() to get the value as a None
course={'language': 'python', 'fee': 4000}
print('duration:', course.get('duration'))

#duration: None

2.3 Return Param Value Example

The param value will be returned if a key is not present in the dictionary, and the value parameter is specified.


# Using get() to get the value as specified
course={'language': 'python', 'fee': 4000}
print('duration:', course.get('duration','Not in dictionary'))

#duration: Not in dictionary

3. Difference Between Python Dictionary get() method and dict[key]

We can also access the values in the dictionary by using the dict[key]. On the other hand, we know that in the get() method if the specified key is not present in the dictionary, it returns a default value(None). similarly, when we use dict[key] if the key is not present in the dictionary an interpreter will be raised a keyerror. Let’s take an example.


# Using get() to get the value as a None
course={'language': 'python', 'fee': 4000}
print('duration:', course.get('duration'))

# Output:
 duration: None

# Use dict[key], key is not in the dictionary
course={'language': 'python', 'fee': 4000}
print('duration:',course['duration'])

# Outputs:
#Traceback (most recent call last):
#file"", line 10, in 
#KeyError: 'duration'

4. Conclusion

In summary, in this article, I have explained the Python Dictionary get() method, using get() you can get the value with the specified key from the dictionary. As well as, I have explained the behavior of get() when the key is not present in the dictionary and using a param value.

Happy Learning !!

References

Related Articles

Leave a Reply

You are currently viewing Python Dictionary get() Method