Dictionaries in Python Image

Data Structures in Python: Dictionaries

Introduction

I’m not going to use a fancy list of Avengers to make my point. I’m still reeling from the ending. Plus, most of the Avengers that they killed off were people of color. My only thought, as I shake my head, is apparently Thanos is racist too. Damn it! The next section of my data type series is dealing with dictionaries in Python.

Dictionaries are useful in a myriad of situations. It’s similar in function to a miniature database, creating small relational models between your values during run time. Most multi-language devs will know them as associative arrays. Instead of using a number to index our values we use keys. These keys can be any immutable object type, strings, numbers or tuples. Key value pairs are an easy way to think about dictionaries. We can do a lot with dictionaries. Where we have to sort through a list, a value can be accessed directly using its key.

Creating a Dictionary

Creating a dictionary is as easy as creating a list:

a = {}
print (a)

Here we assign the name of our dictionary and make it equal to closing brackets. This gives us the following:

{}

Adding Entries to the Dictionary

Creating a list is easy. Adding entries into the list is just as easy. We need to create a dictionary for computer node names. We need to be able to create the computers objects (our keys) and assign them station names which will be used in a database (our values).

computer_list = {}

computer_list["Comp1"] = "Station1"
computer_list["Comp2"] = "Station2"
computer_list["Comp3"] = "Station3"
computer_list["Comp4"] = "Station4"
computer_list["Comp5"] = "Station5"

print (computer_list)

Here we use our opening and closing brackets to create the dictionary. Then we reference our dictionary, and square brackets to add in our key, enclosed in double quotes. The quotes were there because we are using strings as our keys. Then we set that equal to our values. Finally, we print our list, to make sure our work was saved.

{'Comp1': 'Station1', 'Comp2': 'Station2', 'Comp3': 'Station3', 'Comp4': 'Station4', 'Comp5': 'Station5'}

Length

Now that we have our list, we can use the same function we use for lists for our dictionaries to find the number of entries. Usually for looping purposes having the length of a list or dictionary is useful.

print (len(computer_list))

We use the len() function to count out the total entries in our list of computers.

5

Deleting Entries

So we need to delete one of the entries in our list. Turns out one of the computers was moved and they don’t need it.

del computer_list['Comp5']

print (computer_list)

For deleting entries we simply use the del() function then reference the dictionary name and the key of our entry to delete it.

{'Comp1': 'Station1', 'Comp2': 'Station2', 'Comp3': 'Station3', 'Comp4': 'Station4'}

Basic Looping

The intrinsic value of using this type of data type is the ability to loop through our dictionary to perform various tasks with the values. Here I am going to illustrate a simple example of a loop for our dictionary.

Now I need to be able to print out a list of our computers and their station names. To do that we put together a simple loop:

for k, v in computer_list.items():
print(k,v)

Let’s take a quick look. Here we name two placeholders in the for loop to take the place of our keys and our values. We also introduce a new function called the items() function. The items() function is used to loop through dictionary entries. Put all that together and we have our list.

Comp1 Station1
Comp2 Station2
Comp3 Station3
Comp4 Station4

Enumeration

I mentioned earlier that our dictionary does not use numbered indexes like lists do. That’s not completely true. Our entries still have numbers indexes to them. The difference is that we usually refer to the key/value pairs when we need to get to certain entries. We can still use indexes to refer to them. Let’s take a look at the enumerate() function.

for a, b in enumerate(computer_list):
print (a, b)

So we did the same as above and we get the below. Each value showing us the numbered index for the dictionary entry.

0 Comp1
1 Comp2
2 Comp3
3 Comp4

Real World Example

So creating computer names and assigning different stations is no coincidence. I recently had to create a project at work where I was required to have the station names of the computer running the application for database entries. Traceability demanded that we know where our quality people were using the application. So what I did was create a dictionary with the actual computer names which were loaded when the program launched and based on the computer names we assigned station numbers for the entries.

For this purpose, I decided to edit it a little. The purpose of the mini-program is to enter a node name and have the program output the station name or value based on the node name or key.

node_name = input(“Enter computer name:\n”)
node_name = node_name[0].upper() + node_name[1:]

if node_name in computer_list:
print (computer_list.get(node_name))
else:
print(“Please use a designated computer for this program”)
[\python]

So the first part is to create the input, then make sure the first letter is uppercase to conform to our predetermined keys. Then get create the if statement and use the get() function to look into our list for the node name. The get() function takes a key as an argument and searches our dictionary for that key. Then it outputs the value of that key. If the key cannot be found then we get an error message saying that our testers need to use a designated testing station. This also functioned so that only certain workstations can run the application.

Enter computer name:
comp2
Station2

Conclusion

Dictionaries are effective when used correctly. They make for easy ways to keep track of data or variables during run time. Next time we will take a deep dive into another data structure. Maybe hash tables.

The source code for this can be found here

More info on dictionaries can be found here

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply