How to access Python dictionary in simple way?


There are two ways available to access value associated with a key in a dictionary collection object. The dictionary class method get() takes key as argument and returns value.

>>> d1 = {'name': 'Ravi', 'age': 23, 'marks': 56}
>>> d1.get('age')
23

Another way is to use key inside square brackets in front of dictionary object

>>> d1 = {'name': 'Ravi', 'age': 23, 'marks': 56}
>>> d1['age']
23

Updated on: 30-Jul-2019

91 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements