Computer >> Computer tutorials >  >> Programming >> Python

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


You can get the value for a given key from a Python dictionary using the [] operator on the dictionary and passing the key as an argument.

example

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

You can also use the get method on the dictionary to get the value associated to the key in that dict. 

example

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

Output

This will give the output −

TutorialsPoint
15 years