Python Dictionary get() Method

The get() method in Python is a built-in method used to retrieve the value of a specified key in a dictionary. If the key is not found in the dictionary, it returns a default value specified as an optional argument. The get() method is commonly used to avoid KeyError exceptions when accessing values in a dictionary.

Syntax

dict.get(key, default)

Parameters

key: This parameter is required and it specifies the key whose value is to be retrieved from the dictionary.

default (optional): This parameter is optional and it specifies the default value to be returned if the key is not found in the dictionary. The default value is None.

Examples

Example 1: Retrieving the Value of a Key in a Dictionary

Let’s consider a dictionary called student, and we will use the get() method to retrieve the value of the key ‘name’:

student = {'name': 'John', 'age': 25, 'gender': 'Male'}
name = student.get('name')
print(name)

In this example, we have created a dictionary called student, which contains the key-value pairs of a student. Then, we have used the get() method to retrieve the value of the key ‘name’ from the dictionary. Finally, we have printed the value, which will be 'John'.

Example 2: Retrieving the Value of a Key with a Default Value

Let’s consider the following example, where we retrieve the value of a key with a default value:

student = {'name': 'John', 'age': 25, 'gender': 'Male'}
email = student.get('email', 'not found')
print(email)

In this example, we have created a dictionary called student, which contains the key-value pairs of a student. Then, we have used the get() method to retrieve the value of the key ’email’ from the dictionary, but this key does not exist in the dictionary. The default value specified is ‘not found’. Finally, we have printed the value, which will be 'not found'.

Example 3: Modifying the Value of a Key in a Dictionary

Let’s consider the following example, where we modify the value of a key in a dictionary:

student = {'name': 'John', 'age': 25, 'gender': 'Male'}
student['email'] = student.get('email', 'not found')
student['email'] = '[email protected]'
print(student)

In this example, we have created a dictionary called student, which contains the key-value pairs of a student. Then , we have used the get() method to retrieve the value of the key ’email’ from the dictionary and set it to ‘not found’ if the key does not exist. After that, we have modified the value of the key ’email’ to '[email protected]'. Finally, we have printed the entire dictionary, which will return the following output:

{'name': 'John', 'age': 25, 'gender': 'Male', 'email': '[email protected]'}

In this example, we have used the get() method to retrieve the value of a key in a dictionary and also to set a default value if the key does not exist. The get() method is a useful tool to avoid KeyError exceptions when accessing values in a dictionary.

Conclusion

In this article, we have discussed the Python get() method in detail. The get() method is used to retrieve the value of a specified key in a dictionary and also to set a default value if the key does not exist. The get() method is a useful tool to avoid KeyError exceptions when accessing values in a dictionary. We have discussed its syntax, parameters, and provided examples to demonstrate its use in different scenarios. I hope this article has been helpful to you in understanding the Python get() method.

Leave a Reply

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