Access Items

 

Access Items

Accessing Dictionary items:

 

I. Accessing single values:

Values in a dictionary can be accessed using keys. We can access dictionary values by mentioning keys either in square brackets or by using get method.

Example:

info = {'name':'Maxon', 'age':25, 'eligible':True}
print(info['name'])
print(info.get('eligible'))

Output:

Maxon
True

 

II. Accessing multiple values:

We can print all the values in the dictionary using values() method.

Example:

info = {'name':'Maxon', 'age':25, 'eligible':True}
print(info.values())

Output:

dict_values(['Maxon', 25, True])

 

III. Accessing keys:

We can print all the keys in the dictionary using keys() method.

Example:

info = {'name':'Maxon', 'age':25, 'eligible':True}
print(info.keys())

Output:

dict_keys(['name', 'age', 'eligible'])

 

IV. Accessing key-value pairs:

We can print all the key-value pairs in the dictionary using items() method.

Example:

info = {'name':'Maxon', 'age':25, 'eligible':True}
print(info.items())

Output:

dict_items([('name', 'Maxon'), ('age', 25), ('eligible', True)])