Python

Iterate Through Dictionary Python

Just like a regular manual dictionary, a Python dictionary also works in the same way. The dictionary helps the user in storing and manipulating data in different data structures. Items of the dictionary are changeable, ordered, and concurrent. In this article, we will elaborate on the working of Python dictionaries.

Prerequisites

To understand the concept of dictionaries in Python, you need to have any tool to write the source code and execute it. We have used Spyder to do so. We have to run the commands on the Ubuntu terminal. Python is by default part of Linux as it is present in the repositories; if it is not present, you first need to install it.

Common Applications of Python Dictionary

There are a lot of methods of Python dictionaries that are used in performing different operations. Some commonly used dictionary functions are as follows:

Pop ()         It implies the removal of the mentioned key in the dictionary.
Clear ()      It removes all the items present in a dictionary.
Get ()         This function is linked with returning the keys that are defined in the respective code.
Values ()    It returns all the values of dictionary items.

Similar to the functions mentioned earlier, many methods help in iteration in the Python dictionary. The most often utilized ones will be discussed in this article.

Iterate Through Keys in Python Dictionary

This function helps in providing only the view of some data except returning the whole list. To demonstrate this dilemma, we will use an example. Consider a dictionary having the names and subjects of the students. We will first write the code in Spyder and will run or execute this program code in Ubuntu by using a single command in the terminal. Now, let’s have a look at the example source code.

First, after defining the dictionary, we will print only the names in the ‘NamesAndsubject’ as names here act as keys that help in printing the particular key. Therefore, we will pass the “Names” parameter in the print function:

Now, go to the terminal in Ubuntu. Write the following query and then execute it.

$ python 3 ‘/home/aqsa/.config/spyder-py3/temp.py’

The given output depicts the names of the students that refer to the keys of the dictionary. From here, we come to know that the left portion is known as the key portion of any dictionary item.

Iterate Through the Values in the Python Dictionary

Unlike the keys of the dictionary, Values are the right part of the data in the dictionary. In the key example, we have seen that we have used print call to show the keys:

By default, keys are shown in the output. But in the case of values, we need to use the values () function and print the right part that is the subjects of the students. This is shown in the above image.

NamesAndSbject.values()

Now, after that, we will print the subjects. To execute the program and see the results, we will write the command in the Ubuntu terminal. You can also see the results by using the run procedure in the Spyder software. But to see the results in Linux, you need to install Ubuntu. We will use the same command as the file name and directory is for the same file.

The result shows that the correct part is shown as the values of the dictionary.

Iterate Through All Items in the Python Dictionary

Like values and keys, we can also use the whole item, including both (values, keys) in the dictionary to see the desired output. Just similar to the values, to print the items, we will use the item () function and then print both names and subjects.

We can utilize the same query to examine the output. You can see that both values and keys are present in the result as they are part of the items in the dictionary.

Determine and Check If the Key Is Present in the Python Dictionary

If we want to check whether the key is already present in the dictionary or not, we will use the following method. Here we take three attributes in a dictionary named as a sample. We have assign values to these keys. Now we will use the following procedure:

If “Name” in sample :
Print(“yes,……..”)

We want to check the presence of the name key. It is quite a simple method in Python. 

By using the command, we will get the result that shows the key is present. We will use the word Python as the code in this language and then use the filename or the whole directory path. 

Duplicate/Copy the Dictionary

We will take the same example above to demonstrate this duplicate method. Here a new dictionary is defined to copy all the material of the existing dictionary to the new one.

Newsample =sample.copy()

Sample.copy() is the function used in the duplication of all the items.

The output shows the same dictionary items as we have called the new dictionary to print. It’s the simplest way of copying the data from one dictionary to other.

Concept of Nested Dictionaries in Python

As the name implies, the nested dictionary means that the dictionary is inside the dictionary. We have taken an example of details of friends. We have taken friends as a parent dictionary and inside three child dictionaries to elaborate the concept. Each child dictionary contains two attributes(keys) and values. The syntax is straightforward, as we have described below. Parent dictionary is friends and a first, second, and third friend are child dictionaries. We will use only the parent dictionary name to print the data.

While executing, we will see the output that shows all the parent and child dictionaries.

Add Key and Values in Python Dictionary

If we want to add an item in the dictionary, we will simply introduce it by using the following command: 

Friends[‘school’] = ‘highschool’

Now run the query in the Ubuntu terminal, and you will see the addition in the dictionary.

Removal of an Item in Python Dictionary

Now to remove or delete the item from the dictionary, use the keyword “pop”. It is a function to remove a single item through the dictionary. 

Friends.pop(‘school’)

In the output, you will see that the item is removed.

Conclusion

In this guide, we have discussed the different functions of the Python dictionary. This will help to improve your present knowledge. I hope now you can grasp the concept of iterate through a Python dictionary.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.