Methods and Functions

Python Dictionary get()

Let us say you are an ice cream shop owner, and the information for the number of orders placed for chocolate ice cream is in a Python dictionary. How do you retrieve this information? You can use the Python dictionary get() method for this.

The get() method returns the value for a specified key that is present in a dictionary.

Python dictionary get

get() Parameters

The get() method follows the syntax:

                    

dictionary.get (key, value)

Here, it accepts two parameters:

1. Key: The key name of the item you want to find.

2. Value: It is an optional parameter and describes the value that should return if the specified key is not there.

Return Value from get()

If the key you have specified as a parameter is present in the dictionary, get() will return the value associated with that key. If the key is not in the dictionary and you specify a value parameter (optional)–the method returns the specified value. On the other hand, if the key is absent and you did not specify a value parameter, get() returns ‘None‘ value.

Example of How get() works for dictionaries?

Let us look at an example of how the get() method is used with dictionaries.

                    

# create dictionary
you = {'name': 'Chloe', 'age': 50}

print('Name:', you.get('name'))
print('Age:', you.get('age'))

# When a value is not provided
print('Country:', you.get('country'))

# With specified value
print('Country:', you.get('country', 'America'))

Output:

                    

Name: Chloe
Age: 50
Country: None
Country: America

Difference Between Python get() method and dict[key] to Access Elements

You can access the values in the dictionary by using the [] brackets or the get() method. If the specified key is absent in the dictionary, it returns a default value. However, when we use dict[key], the interpreter raises a KeyError exception.

                    

you = {'name': 'Chloe', 'age': 50}

# get()
# results in None value
print('Country:', you.get('country'))

# dict[key]
# results in KeyError
print(you['country'])

Output:

                    

Country: None
Traceback (most recent call last):
  File "", line 9, in 
KeyError: 'country'

Questions and Answers

Q1. What is get () in Python?

Each element in a dictionary is associated with a key that we can use to retrieve that element. Python dictionary get() is a built-in function that allows you to retrieve the value of an item associated with the specified key.

Q2. How do you use the get() method in Python?

We follow the get() syntax:

                    

dictionary.get (key, value)

To retrieve values in a key: value pair element.

For example:

                    

flowers = {'rose': 10, 'lily': 34, 'tulip': 15, 'lotus': 30, 'marigold': 10}
print (flowers.get('lily')

Output:

Q3. How do you access a dictionary value in Python?

There are two ways in which you can access a dictionary value in Python. You can access the values by using the [] brackets or the get() method. If the interpreter does not find the key in the dictionary, it returns:

  • A None value when you use the get() method.
  • KeyError in the case of dict[key].

Q4. How do you print a dictionary in Python?

You can print a dictionary in Python by calling the print() function with the dictionary as the value parameter.

                    

flowers = {'rose': 10, 'lily': 34, 'tulip': 15, 'lotus': 30, 'marigold': 10}
print(flowers)

Output:

                    

{'rose': 10, 'lily': 34, 'tulip': 15, 'lotus': 30, 'marigold': 10}

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

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

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.