Loading...

Python tutorial

Dictionary Methods

The get() method returns the value associated with a key if that key exists in dictionary.If the key does'nt exist, get() returns None,not  an error.

You can also pass a dewfault value to to be returnesd if the key doesn't exist 

print(capitals.get('India')
output:'Delhi'
print(capitals.get('Manipur'))
output:''
print(capitals.get('Manipur','Imphal'))
output:'Imphal'

The methods keys(),values(),and iteams() return different aspects of a dictionary that also help you loop through the dictionary:

print(capitals.keys())
output:dict_keys(['India','Arunachal Pradesh','Assam']
print(capitals.values())
output:dict_values(['Delhi','Itanagar','Dispur']
print(capitals.iteams())
output:dict_items([('India','Delhi'),('Arunachal Pradesh','Itanagar'),('Assam','Dispur')])

Choose one of these methods based on what you're doing with each item.