Get Dictionary Value in Python
Python dictionaries are some of the most fundamental and useful data types. If you are unfamiliar, a Python dictionary refers to a collection of data values expressed as key-value pairs.
This tutorial will walk you through the fundamentals of working with a Python dictionary. We will discuss how you can use the get() function to fetch the value of a Python dictionary.
Python Define Dictionary
Even if you are familiar with the process of defining a new Python dictionary, it does not hurt to recap the basics.
In Python, we can define a dictionary by setting the key-value pairs inside a pair of curly braces. If we have more than one key-value pair, we can separate them using comma.
The following shows the syntax of defining a Python dictionary
dictionary = {key:value, key1:value1}
It is good to keep in mind that each key in a dictionary must be unique in the entire dictionary entries.
Once we have defined a Python dictionary, Python will automatically map each key with the corresponding value.
For example, we can create a Python dictionary that stores the top 5 databases and their default ports like this:
database_ports = {
"MySQL": 3306,
"PostgreSQL": 5432,
"MongoDB": 27017,
"Oracle Database": 1521,
"SQLite": 1433
}
In this case, each database name works as a dictionary while the default port is the corresponding value.
Python Access Dictionary Value
The most simplistic and efficient method accessing the value of a dictionary is by using the dictionary name followed by the target key in square brackets.
The syntax is as shown:
dictionary[key_name]
For example, in our case we can access the default port for a specific database by using its name as the key.
An example code is as shown:
print("Default port for MySQL:", database_ports["MySQL"])
print("Default port for MongoDB:", database_ports["MongoDB"])
Using the Get Method
As you can guess, Python is built on top of simplicity. Hence, it provides us with a method that allows us to automatically retrieve the values mapped to a specific dictionary key. The get()
method.
This method accepts the target dictionary key as the argument and returns the value associated with the key.
The syntax is as shown:
dictionary_name.get(key, value)
It is good to keep in mind that the value specified in the above syntax is not the actual value of the dictionary but the return value if the specified key is not found.
For example, take the database_ports dictionary we defined in the previous steps. We can use the get() method to fetch the default port of a given database as shown in the example below:
database_ports = {
"MySQL": 3306,
"PostgreSQL": 5432,
"MongoDB": 27017,
"Oracle Database": 1521,
"SQLite": 1433
}
database_name = "MySQL" # Replace with the desired database name
default_port = database_ports.get(database_name, "Port not found")
print(f"Default port for {database_name}: {default_port}")
In this example, we use the get()
function to retrieve the default port for the specified database_name.
If the database_name exists in the dictionary, the function returns the corresponding port value. Otherwise, the function will return a string indicating the specified database does not exist.
Conclusion
In this tutorial, we showed you how you can use the provided Python features to retrieve the value of a given dictionary key.