In this Python Dictionary lesson i want to show you Creating Dictionary in Python, a python dictionary is a collection of data values which is unordered, and it is changeable. or we can say a dictionary in Python is a collection of key-value pairs. Each key is connected to a value, and you can use a key to access the value associated with that key. A key’s value can be a number, a string, a list, or even another dictionary. In fact, you can use any object that you can create in Python as a value in a dictionary. In Python dictionaries are written with curly brackets, and they have keys and values.
Let’s create our first dictionary. in this example we have keys for the dictionary and also values to every keys.
1 2 3 4 5 6 7 |
person = { 'name': 'Parwiz', 'lastname': 'Forogh', 'email':'par@gmail.com' } |
This is the result.

Now if you want to access the value of a dictionary, you can use the key of that dictionary.
1 2 3 4 5 6 7 8 9 10 11 |
person = { 'name': 'Parwiz', 'lastname': 'Forogh', 'email':'par@gmail.com' } print(person['name']) print(person['lastname']) print(person['email']) |
Run the code and this is the result.

As with most new programming concepts, using dictionaries takes practice. Once you’ve worked with dictionaries for a bit you’ll soon see how effectively they can model real world situations.
Adding New Key Value Pair
Now let’s learn how to add new key value pairs in python dictionary. Dictionaries are dynamic structures, and you can add new key value pairs to a dictionary at any time. For example, to add a new key value pair, you would give the name of the dictionary followed by the new key in square brackets along with the new value.
1 2 3 4 5 6 7 8 9 10 |
person = { 'name': 'Parwiz', 'lastname': 'Forogh', 'email':'par@gmail.com' } person['phone']='045454545' print(person) |
Creating Empty Dictionary
Some times you need to create empty dictionary, and then add some items to the dictionary, to start filling an empty dictionary, define a dictionary with an empty set of braces and then add each key value pair on its own line.
1 2 3 4 5 6 7 |
worker = {} print(worker) worker['name'] = "john doe" worker['email']='par@gmail.com' print(worker) print(worker['email']) |
Run the code and this is the result.

Updating the value of Dictionary
To modify a value in a dictionary, give the name of the dictionary with the key in square brackets and then the new value you want associated with that key.
1 2 3 4 5 6 7 |
worker = {} print(worker) worker['name'] = "john doe" worker['email']='par@gmail.com' worker['email'] = 'updated@gmail.com' print(worker['email']) |
This is the result.

Removing Key Value Pairs
When you no longer need the value that’s stored in a dictionary, you can use the del keyword to remove a key value pair.
1 |
del worker['email'] |
Using get() to Access Values
Using keys in square brackets to retrieve the value from a dictionary might cause some problems, if the key doesn’t exist, you’ll get an error, so we can use get to handle this error.
1 |
print(worker.get('email', 'No email key exists')) |