Python Get Dictionary Value from Key with examples

Hello!

We are going to get values from a dictionary using keys in python.

Code steps:

  1. Initialize a dictionary with keys and values
  2. Access the values by using the keys of the dictionary.
  3. Storing it in a variable
  4. Printing the values on the console

Here is a dictionary with keys and values

#Initialized Dictionary
dictionary_var={
    'Key':'Value',
    'Website':'Collegelib',
    'Subject':'Programming',
    'Program':'Python'
    }

Now we will make a variable to store the value, and then get the key using the following syntax:

variable_to_store_value = dictionary_name[‘Key_name’]

Syntax

And print it. So the code should be looking like this:

Example 1

#Storing and printing the accessed values

website_value = dictionary_var['Website']
print(website_value)

#Output:
Collegelib

Example 2


#Storing and printing the accessed values
subject_value = dictionary_var['Subject']
print(subject_value)

#Output: 
Programming