DEV Community

Cover image for Data Structures in Python: Dictionaries
George Offley
George Offley

Posted on

Data Structures in Python: Dictionaries

This article can also be found on my blog here


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'}


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.


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():

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


There is actually an update. To clarify there is no numeric index only that the enumerate function gives us a list of the values in a numbered list. They can't actually be used to grab the key/value. Tried it out a couple times to make sure. Thanks to Andrew for correcting me. I'm glad we can use these posts as a way to get a real discussion going.

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))
    print("Please use a designated computer for this program")

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:


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

Discussion (5)

doshirae profile image

In that last exemple of code here :

if node_name in computer_list:
    print (computer_list.get(node_name))
    print("Please use a designated computer for this program")

The get fonction has a second argument, that is a default if getting the first argument doesn't exist in the dict !

print (computer_list.get(node_name, "Please use a designated computer for this program"))
georgeoffley profile image
George Offley Author

Good to know, I'm used to using the what I had.

gbm001 profile image
Andrew McLeod • Edited on

Firstly, you rarely use the 'get' method for dictionaries. Normally you just use the square bracket notation for reading value. In your case, temporarily ignoring that it might be better to use 'get' with a default argument you could do:

if node_name in computer_list:
    print (computer_list[node_name])
    print("Please use a designated computer for this program")

Secondly, dictionaries do not have numeric indexes. They also do not have a defined order anyway. Enumerate takes an iterable (such as what is returned by dict.items()) and returns a list of (index, item) tuples. But you can't use the index returned from enumerate on a dictionary.items to access the dictionary items. You can't even guarantee that running it twice will produce the same order of items: because items are dictionaries are unordered.

It is also a lot less repetitive to just say:

my_dict = {key1: value1,
           key2: value2,
           key3: value3}

rather than making repeated

my_dict[keyN] = valueN

Finally you haven't said whether you are doing python 2 or python 3 (which has been out for 10 years now...). In python 2 dict.items will return a list of (key, value) tuples (which you can loop over as you do. In python 3 items returns a generator object (like iteritems in python 3). You can still loop over this, but if you want the list of (key, value) tuples you have to call list(dict.items). This is a common source of confusion for people switching from python 2 to python 3...
Finally finally you should probably say that dictionary keys can be any hashable object - e.g. integers, floats (although that is a bad idea), strings, tuples and user-defined classes.

georgeoffley profile image
George Offley Author • Edited on

I updated the enumeration section. Thanks a lot for your input. On the rest the print() method is usually my tip off on Python2 or 3. While I understand that there can be confusion, anything made in the last year or so should be in Python 3. Anything I write is in Python3 and tested in Python3.

The rest is really style, and there's no substance in writing out adding entries to a dictionary one way or the other. I will say that not repeating yourself is a golden rule in programming. However, for this purpose it doesn't matter.

Finally the get function is only rare if I use it rarely. Unless there is a specific performance issue with using that particular function, I don't think it matters. Thanks for the input, though.

paddy3118 profile image

Del is a python statement not a function. :-)