Both lists and dictionaries can retain structured data in Python. The dictionary is unique in that it stores individual entries with human-readable logical names rather than index numbers.
Following the previous discussion about data storage in the list format, the next most common type of data structure in Python is the dictionary. These new data types provide slightly more structure to contained objects than lists and closely resemble JSON strings.
What is a Dictionary?
The dictionary data structure is similar to a list in several main characteristics. Both are mutable, and they both retain the order of objects within them, a factor that was critical to the “Jet Fuel Mercaptan Oxidation Treating” process example in the last section.
However, dictionaries differ from lists in that they use a key instead of a numeric index to mark the offset of objects within them. We will continue with the same example we used previously as we now highlight how the process may be viewed through the lens of a dictionary.
Figure 1. A chemical process diagram used to relate concepts of a Python dictionary.
Using the handy chemical process flow in Figure 1 again, each of the equipment objects in the process should be viewed as a collective whole. A dictionary allows us to group objects together just as the list does.
Defining the dictionary involves declaring a variable assigned to curly brackets { }. Let’s define the process above using a dictionary and then dive into the specifics:
Dictionaries are composed of key value pairs very similar to a JSON structure. The keys are restricted to immutable data types such as strings, integers, booleans, and floats. Values may take the shape of both immutable and mutable types such as another list or dictionary nested inside the dictionary. In the jet_fuel_mercaptan_oxidation_treatment the keys are denoted by ‘equipment_’ and the values are strings denoting the name of the equipment.
A good way to keep dictionaries aesthetically clean and easy to read is to define the first curly bracket and then create a tabbed new line containing the key value pair. The comma is required to separate each key value pair in the dictionary with the last pair in the dictionary not requiring a comma. Each subsequent key value pair should have its own new line.
Working with Dictionaries
Accessing an Entry
Dictionaries, like lists, preserve the order of objects. Order of equipment in the jet fuel mercaptan oxidation process is important to obtain the desired sweetened jet fuel product. However, instead of specifying a numeric offset, dictionary objects are accessed by indicating the key name:
Figure 2. Accessing an indicated data value by recalling a key name.
Duplicate Keys
What would happen if we had 2 keys of the same name? Wouldn’t pulling the associated value cause a conflict? Let’s try it out for a duplicate “equipment_1” key:
Figure 3. An example of an error-prone duplicate key name.
The return will be the second key overwriting the first key, as shown above. However, dictionaries should not contain duplicate keys; notice how the Pycharm editor has issued yellow squiggly lines underneath ‘equipment_1?’ It is a warning that duplicate keys are a bad idea:
Figure 4. Error resulting from a duplicate key name.
Updating a Value
Let’s say that our engineer once again wants to update the “Salt Bed” equipment name to “Sodium Chloride Bed”:
Figure 5. Editing the name of a variable in a data set.
We update the value by accessing the existing key name (‘equipment_1’) and reassigning it with the value of “Sodium Chloride Bed”.
Adding a Key Value Pair
The engineer managing this process wants to add a second stage of water wash equipment to the process. This can be accomplished with the same syntax used to update an existing key’s value:
Figure 6. Adding a new entry to the data set.
Deleting a Key Value Pair
After adding the second stage clay bed, the engineer realizes the new equipment added is much more efficient than the original clay bed assigned to the ‘equipment_6’ key. Deleting the original ‘equipment_6’ key from the process can be accomplished with the del keyword:
Figure 7. Deleting an entry from the value set.
Nested Dictionaries
From an engineering perspective, a benefit of using the dictionary structure is the ability to assign additional metadata to objects. In the jet_fuel_mercaptan_oxidation_treatment chemical process, the equipment objects have many more associated attributes than just a textual name. For example, temperature and pressure are often the key parameters to a successful chemical reaction. An engineer managing this process could log and display the temperature and pressure parameters of the Merox reactor by using a nested dictionary:
The actual values would likely be an integrated real time data feed, but for the scope of this article we will harcode integer values.
Accessing nested dictionary keys requires slightly different syntax. Let’s access the pressure value of the reactor:
Figure 8. Accessing a value nested within a parent data entry.
Dictionary Methods
The dictionary, like the list, is a built-in data structure to the Python language. The dictionary data structure has predefined methods that can be called to perform actions on instances of dictionary objects. This section will not cover all dictionary methods, but will provide the two most common I’ve experienced in courses and world use.
Figure 9. Visual of built-in dictionary methods available in Python.
.keys()
This method will return all of the keys associated with a dictionary assigned to the variable in the form of a list.
The output:
.values()
This method returns all of the values associated with a dictionary assigned to the variable in the form of a list.
The output:
Wrapping Up
In the next (forthcoming) section of our Python tutorial series, we’ll continue the data structures chapter of the Python learning journey with tuples.
Did you miss the earlier articles?
Part 1 | What is Python and Where do I Start?
Part 2 | Basic Data Types
Part 3 | Data Structures: Lists
Copyright Statement: The content of this website is intended for personal learning purposes only. If it infringes upon your copyright, please contact us for removal. Email: admin@eleok.com