Data Structures in Python: Dictionaries
Tags: python, datastructures, datatypes, dictionaries
Published: 2018-07-15 03:53:44

---
title: Data Structures in Python: Dictionaries
published: true
description: Part two of my series on data structures in Python 
tags: #python #data structures #data types #dictionaries
cover_image: https://upload.wikimedia.org/wikipedia/commons/thumb/e/e2/English-English_and_English-Persian_dictionaries.JPG/1200px-English-English_and_English-Persian_dictionaries.JPG
---

*This article can also be found on my blog [here](https://www.georgeoffley.com/new_dev/2018/07/15/data-structures-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:

```python
a = {}
print (a)
```

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

```text
{}
```

**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).

```python
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.

```text
{'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.

```python
print (len(computer_list))
```

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

```text
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.

```python
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.

```text
{'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:

```python
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.

```text
Comp1 Station1
Comp2 Station2
Comp3 Station3
Comp4 Station4
```

**Enumeration**

*Update*
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](https://dev.to/gbm001) for correcting me. I'm glad we can use these posts as a way to get a real discussion going.

```python
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.

```text
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.

```python
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")
```

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.

```text
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](https://github.com/georGEO1989/Data_structures_Dictionaries/blob/master/source.py)

More info on dictionaries can be found [here](https://docs.python.org/3/tutorial/datastructures.html#dictionaries)