Python Dictionary Get() Method | Explained

In Python, different data structures are used to store the value like list, tuple, dictionary, etc. Dictionary is used to store the data in the “key-value” pair. The dictionary items are accessed by applying the “get()” method to these key values. The “get()” method is used in python to get the value of the dictionary “key” and also assign the value if it is not present in the elements of the dictionary.

This write-up will explain the dictionary “get() method” concepts using numerous examples. Following are the main aspect of this post:

So let’s get started!

What is Python Dictionary get() Method?

Python’s “get()” method gets the value of a specific “key” from the dictionary. Dictionary is used when we want to store some specific data because in the dictionary, all the values have a key name, so we can access the value by calling their “key” name using the“get() method”.The syntax of this method is shown below:

dictionary.get(keyname, value)

The “get() method” has two parameters:

  • Keyname: Every item in the dictionary has a specific key name which is used throughout the program to call the value.
  • Value: This parameter initializes the value that does not exist in the elements of the dictionary. The value will be none by default.

Let’s understand the concept of the Dictionary “Get()” method with an example given below:

Example 1: Access Key Value

Python Dictionary “get()” method is used to access the value of a different key by calling their “key” names using the “get()” method.

Let’s see an example code given below:

Code

#getting the value from the dictionary set
student = {"name": "Ali","gender": "Male","age": 19}

val_1 = student.get("name")
val_2 = student.get("age")
print(val_1)
print(val_2)

In the above code:

  • The dictionary variable named “student” is initialized.
  • The “get” method is used to access the items of the dictionary by mentioning their “key” name inside the parenthesis like the “name” and “age”.

Output

The output accessed the value of the name and age from the dictionary variable “student”.

Example 2: Return Key Value

Python dictionary “get()” method returns the value of a key by passing its value into the parameter. The value will be the default “none” if we return the key not present in the dictionary.

Let’s understand the code given below:

Code

student = {"name": "Ali","gender": "Male","age": 19}
val_1 = student.get("name")
val_2 = student.get("age")

print("Name of Student: ", val_1)
print("Age of Student: ", val_2)

val_3 = student.get("class",)
print("Class of Student: ", val_3)

val_3 = student.get("class", 9)
print("Class of Student: ", val_3)

In the above code:

  • The variable named “student” is initialized using a dictionary.
  • The “get() method” is used to call the value by using the key of dictionary items “name” and “age
  • Now if we want to return any value by using the “get() method” we pass the key name with their value inside the parenthesis.
  • The “get() method” will return “nothing” when there is no value assigned to the input key like “student.get(“class”, )
  • When the value is provided like “student.get(“class”, 9)” then at the output the get() method will return its value.

Output

In the above output, the “get() method” accessed the value from the dictionary using the keys.

That’s all from the Python “get()” method.

Conclusion

In Python, the Dictionary “get() method” is used to access the value of the key. In the dictionary, every value is stored in the form “key-value” pair. To access the specific value, the “key name” is to be called in the program. The “get() method” also returns the value using its “value” parameter and returns “none” if the value is not specified. This post has briefly explained the concept of the dictionary “get() method” with an appropriate example.