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:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/iterate-through-dictionary-python-01.png" data-lazy- height="318" src="data:image/svg xml,” width=”668″>

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

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

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/iterate-through-dictionary-python-02.png" data-lazy- height="202" src="data:image/svg xml,” width=”974″>

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:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/iterate-through-dictionary-python-03.png" data-lazy- height="316" src="data:image/svg xml,” width=”620″>

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.

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/iterate-through-dictionary-python-04.png" data-lazy- height="202" src="data:image/svg xml,” width=”974″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/iterate-through-dictionary-python-05.png" data-lazy- height="362" src="data:image/svg xml,” width=”679″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/iterate-through-dictionary-python-06.png" data-lazy- height="200" src="data:image/svg xml,” width=”974″>

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. 

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/iterate-through-dictionary-python-07.png" data-lazy- height="254" src="data:image/svg xml,” width=”570″>

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. 

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/iterate-through-dictionary-python-08.png" data-lazy- height="60" src="data:image/svg xml,” width=”974″>

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.

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

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/iterate-through-dictionary-python-09.png" data-lazy- height="306" src="data:image/svg xml,” width=”633″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/iterate-through-dictionary-python-10.png" data-lazy- height="60" src="data:image/svg xml,” width=”974″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/iterate-through-dictionary-python-11.png" data-lazy- height="449" src="data:image/svg xml,” width=”604″>

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

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/iterate-through-dictionary-python-12.png" data-lazy- height="72" src="data:image/svg xml,” width=”974″>

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’

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/iterate-through-dictionary-python-13.png" data-lazy- height="302" src="data:image/svg xml,” width=”637″>

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

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/iterate-through-dictionary-python-14.png" data-lazy- height="60" src="data:image/svg xml,” width=”974″>

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. 

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/iterate-through-dictionary-python-15.png" data-lazy- height="300" src="data:image/svg xml,” width=”610″>

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

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/iterate-through-dictionary-python-16.png" data-lazy- height="58" src="data:image/svg xml,” width=”974″>

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.