Dictionaries in Python

Dictionaries in Python can be created by using curly braces as below:

phone_nos = {}

This is an empty dictionary.

We can add an item like below:

phone_nos['sreedhar'] = 123456
phone_nos['sam'] = 1781767

A dictionary contains keys and values. In the above example, ‘sreedhar’ and ‘sam’ are keys and 123456 and 1781767 are values.

We get the value of a key as below:

print(phone_nos['sreedhar'])

We can print all the keys using:

print(phone_nos.keys())

We can loop through a dictionary and print the keys and values as below:

for name in phone_nos:
    print(name, phone_nos[name]

Google “Python dictionary” and try to learn more.