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: 2019-07-30T22:30:21+05:30

152 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements