Python Tutorial – Chapter 9 – Python Dictionary (Key : Value Pairs)

In this Chapter 9 of Python Tutorial, we will be learning about Python Dictionary (Key : Value Pairs)

If you are reading this tutorial on Mobile, please rotate your mobile horizontally for best reading experience. You can enable auto-rotate in your phone settings.

In Python, a dictionary is a data type, which consists of a key and a value. A key, which can be any kind of object (a string, a number, a list, etc.), can be used to access any value that is stored in a dictionary. In simple words, dictionary is a data structure that stores the value in Key : Value pairs. Syntax is as below:

dict_var = {key1 : value1, key2 : value2, …..}

For example, a database of an employee could be stored using a dictionary like this:

DataCamp Light | Standalone example
student_marks = {} student_marks["Alex"] = 95 student_marks["Rick"] = 71 student_marks["Mary"] = 83 print(student_marks)

In the example above, we created an empty dictionary and then started adding keys and values to it.

The keys are Alex, Rick, and Mary. Their associated values are 95, 71, and 83, respectively.

When there are multiple key-value pairs in a dictionary, each key-value pair is separated with a comma,.

One way of adding key-value pair to dictionary is mentioned above, alternatively, key-value pairs can be added in the following way:

DataCamp Light | Standalone example
student_marks = { "Alex" : 95, "Rick" : 71, "Mary" : 83 } print(student_marks) #Let's add two more students to the database student_marks = { "Alex" : 95, "Rick" : 71, "Mary" : 83, "David" : 57, "Liv" : 65 } print(student_marks)

A dictionary does not maintain the order of the values it stores. So, iterating over it is little different.

Iterating over key-value pairs in a dictionary, requires two variable specifications and also, need to use the .items() method. Let’s take a look at the example below:

DataCamp Light | Standalone example
student_marks = { "Alex" : 95, "Rick" : 71, "Mary" : 83 } for name,marks in student_marks.items(): print("%s scored %d in the Finals" %(name,marks))

we have already seen how to add (refer the 1st example). If you would like to remove an item from the dictionary. Let’s look at below example:

DataCamp Light | Standalone example
student_marks = { "Alex" : 95, "Rick" : 71, "Mary" : 83 } del student_marks["Rick"] print(student_marks) #similar way to remove the item student_marks = { "Alex" : 95, "Rick" : 71, "Mary" : 83 } student_marks.pop("Rick") print(student_marks)

Both the options to remove item from dictionary will work.

If there is a request to update/change the value in a dictionary, we can do so by referring to its key. For example:

DataCamp Light | Standalone example
student_marks = { "Alex" : 95, "Rick" : 71, "Mary" : 83 } #change the value "Rick" to 79 student_marks["Rick"] = 79 print(student_marks)

Assignment:

Add “Peter” to the student_marks with marks 43. Remove “Rick” from student_marks.

DataCamp Light | Standalone example
student_marks = { "Alex" : 95, "Rick" : 71, "Mary" : 83 } #Write your code here #testing code print(student_marks) student_marks = { "Alex" : 95, "Rick" : 71, "Mary" : 83 } #Write your code here student_marks["Peter"] = 43 del student_marks["Rick"] #testing code print(student_marks)

That brings to the end of chapter 9 of Python tutorial, see you in the next tutorial!!!

Please make sure your practice your learnings in the Python IDE. Practice makes everyone Perfect!

By the way, if you haven’t installed Python and Pycharm IDE on your Windows laptop, you can visit the below links to learn on how to install and configure/install Python and Pycharm IDE on Windows OS. This might be helpful in understanding the Python language easier.

How to Install Python 3 and Pycharm IDE on Windows

How to configure Pycharm IDE and “Run” your first Python 3 program

For your reference, to download Pycharm IDE, visit below link:

https://www.jetbrains.com/pycharm/download/?section=windows

However, if you don’t want to go through installation process, you can practice your learnings on this page itself by using integrated Python IDE above.

If you have any questions or comments about chapter 9 of Python tutorial, please let us know in the comments section below.

Leave a Reply