How to get a value for a given key from a Python dictionary?



In this article, we will show you how to get a value for the given key from a dictionary in Python. Below are the 4 different methods to accomplish this task -

Assume we have taken a dictionary containing key-value pairs. We will return the value for a given key from a given input dictionary.

Using dictionary indexing

In Python, we can retrieve the value from a dictionary by using dict[key].

Example 1

The following program returns the index of the value for a given key from an input dictionary using the dict[key] method -

# input dictionary
demoDictionary = {10: 'TutorialsPoint', 12: 'Python', 14: 'Codes'}

# Printing the value of key 10 from a dictionary
print("The value of key 10 from dictionary is =", demoDictionary[10])

When you run the program, it will show this output -

The value of key 10 from dictionary is = TutorialsPoint

If the key does not exist in a given input dictionary, a KeyError is raised.

Example 2

In the below code, the key 'hello' is not present in the input list. So, when we try to print the value of the key 'hello', a KeyError is returned -

# input dictionary
demoDictionary = {10: 'TutorialsPoint', 12: 'Python', 14: 'Codes'}

# Printing the value of key 'hello' from a dictionary
# A KeyError is raised as the hello key is NOT present in the dictionary
print("The value of key 'hello' from a dictionary:", demoDictionary['hello'])

After running the program, you will get this result -

Traceback (most recent call last):
  File "main.py", line 6, in 
    print("The value of key 'hello' from a dictionary:", demoDictionary['hello'])
KeyError: 'hello'

Handling the KeyError

The following code handles the KeyError returned in the above code using the try-except blocks -

Example

Here, the except block statements will get executed if any error occurs -

# input dictionary
demoDictionary = {10: 'TutorialsPoint', 12: 'Python', 14: 'Codes'}

# Handling KeyError using try-except blocks
try:
   print(demoDictionary['hello'])
except KeyError:
   print("No, The key for Value 'Hello' does not exist in the dictionary. Please check")

This output will be displayed when the program runs -

No, The key for Value 'Hello' does not exist in the dictionary. Please check

Using dict.get() method

We can get the value of a specified key from a dictionary using the get() method of the dictionary without throwing an error if the key does not exist.

As the first argument, specify the key. If the key exists, the corresponding value is returned; otherwise, None is returned.

Example 1

The following program returns the index of the value for a given key from an input dictionary using dict.get() method -

# input dictionary
demoDictionary = {10: 'TutorialsPoint', 12: 'Python', 14: 'Codes'}

# Printing the value of key 10 from a dictionary using get() method
print("The value of key 10 from a dictionary:", demoDictionary.get(10))

# Printing the value of key 'hello' from a dictionary
# As the hello key does not exist in the dictionary, it returns None
print("The value of key 'hello' from a dictionary:", demoDictionary.get('hello'))

You will see this result after executing the program -

The value of key 10 from a dictionary: TutorialsPoint
The value of key 'hello' from a dictionary: None

In the second argument, you can define the default value to be returned if the key does not exist.

Example 2

The following program returns the user-given message, which is passed as a second argument, if the key does not exist in the dictionary using dict.get() method -

# input dictionary
demoDictionary = {10: 'TutorialsPoint', 12: 'Python', 14: 'Codes'}

# Printing the value of key 'hello' from a dictionary
# As the hello key does not exist in the dictionary, it returns the user-given message
print("The value of key 'hello' from a dictionary:", 
demoDictionary.get('hello', "The Key does not exist"))

The program gives this output when it is run -

The value of key 'hello' from a dictionary: The Key does not exist

Using keys() Function

The dict.keys() method returns a view object that displays a list of all the keys in the dictionary in order of insertion.

To retrieve a value for a given key, we need to check if the given key is equal to the iterator value by comparing each key to the desired one, and if it is equal, then print its corresponding value.

Example

The following program returns the index of the value for a given key from an input dictionary using the keys() method.

# input dictionary
demoDictionary = {10: 'TutorialsPoint', 12: 'Python', 14: 'Codes'}

# enter the key for which the value to be obtained
givenkey= 10

# Traversing the keys of the dictionary
for i in demoDictionary.keys():

   # checking whether the value of the iterator is equal to the above-entered key
   if(i==givenkey):

      # printing the value of the key
      print(demoDictionary[i])

Running the program will produce the following output -

TutorialsPoint

Using items() Function

Here we are using the items() function, which is used to return a view object that displays a list of a dictionary's key-value tuple pairs. The view object is dynamic and will reflect any changes made to the dictionary.

Example

The following program returns the index of the value for a given key from an input dictionary using the items() method -

# input dictionary
demoDictionary = {10: 'TutorialsPoint', 12: 'Python', 14: 'Codes'}
givenkey= 12

# Traversing in the key-value pairs of the dictionary using the items() function
for key,val in demoDictionary.items():

   # checking whether the key value of the iterator is equal to the above-entered key
   if(key==givenkey):
      print(val)

The result of this Python program is -

Python
Updated on: 2025-06-11T12:47:53+05:30

34K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements