How to access Python dictionary in simple way?

PythonServer Side ProgrammingProgramming

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
raja
Published on 22-Feb-2018 05:50:18
Advertisements