How to Create and Access a Dictionary in Python



Python


In this article, we show how to create and access the elements of a dictionary in Python.

A dictionary in Python is really an associative array or hash table that is composed of key-value pairs. So if you have a dictionary called student, for example, you may a key be a name with the value being 'Tom', another key being age with the value being 12, another key being gender with the value being 'male'.

If you are familiar with PHP, a dictionary is really an associative array. Python uses the term dictionary.


How to Create a Dictionary in Python

To create a dictionary in Python, braces are used to enclose the entire dictionary ({ }).

We create a dictionary called student shown below.



So, you can see the dictionary created above which is called student. It has 3 key-value pairs. One is "name" which has a value of "Tom". Another is "age", which has a value of 12. Another is "gender", which has a value of "male".


How to Access the Elements of a Dictionary in Python

Once we have created a dictionary, we now need to know how to access the elements of the dictionary.

This is done through the general format, dictionary_name['key'].

Below is the code to access each element of the dictionary we created below.

So, now you can see how to access the elements of a dictionary in Python.


How to Insert an Element into a Dictionary in Python

So let's say we've created a dictionary in Python. We'll go over the same dictionary we created before, student. ==

So let's say we want to add another attribute. Let's say the student's phone number.

You do not have to recreate the entire dictionary. You can simply add in this new attribute.

This is done through the general format, dictionary_name['key']= 'value'



Thus, you can see that this is now a part of the student dictionary.


How to Modify an Element in a Dictionary

Also, you may want to modify an existing element in a dictionary.

Again, you don't have to rewrite the whole dictionary, but just change that one element.

Let's say we have the same student dictionary again.

But the student has now turned 13 years old. The student just had his birthday. We want to update the student's age from 12 to 13.

We can just modify the age element.

This is shown in the code below.



So you can see that we've successfully modified the value of the age element in the dictionary.


How to Delete an Element from a Dictionary

To delete an element from a dictionary in Python, you use the del keyword followed by the element you want to delete.

So the code to delete the gender element from the student dictionary would be the following shown below.



So, from the student dictionary, we delete the gender element using the del keyword followed by the element.

Now, if we list the keys of the dictionary, we see that the gender element has been removed.

So, you can see that it's relatively simple to create a dictionary, access its elements, insert a new element into the dictionary, modify an existing element, and delete an element in Python.


Related Resources

How to Show All Tables of a MySQL Database in Python

How to Count the Number of Rows in a MySQL Table in Python



HTML Comment Box is loading comments...