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

PythonProgrammingServer Side Programming

An unordered and changeable mapping of distinct keys to values is maintained in a dictionary, a Python container. Dictionaries are used to store data values in key-value pairs. Curly brackets are used when writing dictionaries, and they contain keys and values.

As of Python 3.7, dictionaries are now sorted. Python versions 3.6 and earlier do not sort dictionaries.

Example

This example shows how a basic dictionary can be used in python.

thisdict = { "companyname": "Tutorialspoint", "tagline" : "simplyeasylearning", } print(thisdict)

Output

The above code produces the following results.

{'companyname': 'Tutorialspoint', 'tagline': 'simplyeasylearning'}

In this article, we find the value of a given key from a dictionary in python.

Specify the key within []

In this method, we specify the key name inside the [] to get the value associated with the particular key.

Here if the key is not present inside the dictionary then an KeyError is popped.

Example 1

This example shows how a basic dictionary can be used in python.

thisdict = { "companyname": "Tutorialspoint", "tagline" : "simplyeasylearning", } print(thisdict)

Output

The above code produces the following results.

{'companyname': 'Tutorialspoint', 'tagline': 'simplyeasylearning'}

In this article, we find the value of a given key from a dictionary in python.

Specify the key within []

In this method, we specify the key name inside the [] to get the value associated with the particular key.

Here if the key is not present inside the dictionary then an KeyError is popped.

Example 1

In the following example, one key is present in the dictionary whereas the other key is not in the dictionary so an error occurs.

this_dict = { "companyname" : "Tutorialspoint", "tagline" : "simplyeasylearning" , 'location': 'India'} print(this_dict['companyname']) print(this_dict['address'])

Output

The above code produces the following results;

Tutorialspoint
Traceback (most recent call last):
File "<string>", line 3, in <module>
KeyError: 'address'

Example 2

Following is another example to retrieve value of a given key using the “[]” operator −

my_dict = {'name': 'TutorialsPoint', 'time': '15 years', 'location': 'India'} print(my_dict['name']) print(my_dict['time'])

Output

This will give the output −

TutorialsPoint
15 years

Using the get() method

In this method, we use the get() method to know if the key is present in the dictionary or not. The get() method returns the value of the item with the specified key.

Syntax

The syntax of the get() method in python is as follows.

dict.get(key,value)

Where,

  • key - The key name of the object from which you want to retrieve the value.
  • value - If the given key does not exist, this value is returned. The default value is None.

Example 1

In the following example code, we used the get() method to find value when key is specified. Here, the key ‘companyname’ is present in the dictionary so it returns the value associated with it. The key ‘address’ is not in the dictionary so it returns None value.

this_dict = { "companyname" : "Tutorialspoint", "tagline" : "simplyeasylearning" , 'location': 'India'} print(this_dict.get('companyname')) print(this_dict.get('address'))

Output

The following is the output for the above code;

Tutorialspoint
None

Example 2

Following is another example to retrieve value of a given key using the get() method −

my_dict = {'name': 'TutorialsPoint', 'time': '15 years', 'location': 'India'} print(my_dict.get('name')) print(my_dict.get('time'))

Output

TutorialsPoint
15 years
raja
Updated on 05-Sep-2022 10:20:45

Advertisements