Python dictionary get() method [With Examples]

In this Python article, we will explore the Python dictionary get() method, which is a valuable tool for safely accessing dictionary values. We will discuss its syntax, usage, and some practical examples to help you utilize this method effectively.

Below are the topics that we are doing to discuss in this article:

  • Introduction to Python Dictionary get() method
  • Syntax of the get() method
  • Purpose and use cases of the get() method

Dictionary get() method in Python

The get() method in a Python dictionary is a built-in function that allows you to safely access a value from a dictionary using a key without raising a KeyError if the key is not found. Instead, it returns a default value provided by you, or None if no default value is specified.

The syntax for the get() method is as follows:

dictionary.get(key, default)
  • dictionary: The dictionary we want to access.
  • key: The key for which we want to retrieve the corresponding value.
  • default (optional): The value to be returned if the specified key is not found in the dictionary. If not provided, it defaults to None.

Return Value:

The method returns the value associated with the specified key if it exists in the Python dictionary; otherwise, it returns the default value provided.

get() method in Python Dictionary Examples

Let’s take a look at some examples of using the get() method.

Example#1 Basic Usage

presidents_terms = {"George Washington": [1789, 1797], "John Adams": [1797, 1801], "Thomas Jefferson": [1801, 1809]}

print(presidents_terms.get("George Washington", "Unknown"))
print(presidents_terms.get("James Monroe", "Unknown"))       

In this example, we have a Python dictionary containing the names of US presidents as keys and their term years as values.

Using the get() method, we can safely retrieve the term years for a given president, or return "Unknown" if the president is not in the dictionary.

Output:

Python dictionary get method
Python dictionary get method

Example#2 Using get() with a Default Value

state_capitals = {"California": "Sacramento", "New York": "Albany", "Texas": "Austin"}

print(state_capitals.get("California", "Not Found")) 
print(state_capitals.get("Florida", "Not Found"))     

In this example, we have a Python dictionary of US states and their capitals. By using the get() method, we can look up the capital of a given state, or return "Not Found" if the state is not in the dictionary.

Output:

Python dictionary get method example
Python dictionary get method example

Example#3 Using get() Without a Default Value

city_population = {"New York": 8_336_817, "Los Angeles": 3_979_576, "Chicago": 2_693_976}

print(city_population.get("New York")) 
print(city_population.get("Houston")) 

Here, we have a Python dictionary of US cities and their populations. Using the get() method, we can retrieve the population of a given city, or return None if the city is not in the dictionary.

Output:

Dictionary get method in Python
Dictionary get method in Python

Read Python dictionary fromkeys() method [With Examples]

Example#4 Counting Word Frequency

text = "Python is easy to learn. Python is a powerful language."
word_frequency = {}

for word in text.split():
    word_frequency[word] = word_frequency.get(word, 0) + 1

print(word_frequency)

In this example, we use the Python get() method to count the frequency of each word in a given text. By initializing the word frequency to 0 using the get() method, we can easily update the frequency count for each word in the Python dictionary.

Output:

Dictionary get method in Python example
Dictionary get method in Python example

Conclusion

The Python dictionary get() method is a powerful and convenient way to access values in a dictionary. By using the get() method, you can avoid KeyError exceptions, improve code readability, and make your code more robust.

You may also like to read the following articles: