Essential Dictionary Mutation For Python Programmers | by Emmett Boudreau | Towards Data Science

The dictionary data-type is arguably the most important data structure in the Python programming language. This is even more true for Data Scientists who often work with labeled data that works exactly the same as a dictionary. In many ways, data frames are simply tabular views for the exact same thing that a dictionary does along with some helpful functions for data processing.
The data structure also serves as the foundation of JSON, which is a very popular data format both for storage and transmission via requests. Python has a pretty awesome implementation of the dictionary data structure, and there are a lot of things that the data structure is capable of doing out of the box. Needless to say, knowing the ins and outs of the dictionary data structure is going to come in handy in Data Science work. There is a lot to learn, but today I wanted to review some of the dictionary methods that I think are very exceptional and useful, especially for Data Science.
notebook
We have all heard of sorting Pandas series and data frames, but did you know you can do essentially the same thing with a dictionary? We can sort both by by the alphabet and also by numerical value. This is convenient because it allows us to sort all keys and their pairs at the same time. This can also be done with tuples and other data types, but for now we will stick to the dictionary. Consider the following dictionary:
cities: dict = {    "Detroit": 3619342323,    "Las Vegas": 99870,    "Las Angeles": 9525067,    "Boulder": 17098246,    "Atlanta": 2780400,    "Paducah": 752612,    "Tampa": 9596961,}
We can sort this dictionary using the sorted() method along with a dict cast. For the key, we will use the numerical values in our dictionary. In order to do this with each key, we will need to use a lambda expression in order to evaluate the key for each value. If you would like to learn more about lambda and what it is used for, I have an article I wrote on it a while back that I think is worth a read! You can check out that article here:
towardsdatascience.com
Let us go ahead and sort our dictionary:
citysorted = dict(sorted(cities.items(), key = lambda kv: kv[1], reverse=False))
Now our dictionary looks like this:
{'Las Vegas': 99870, 'Paducah': 752612, 'Atlanta': 2780400, 'Las Angeles': 9525067, 'Tampa': 9596961, 'Boulder': 17098246, 'Detroit': 3619342323}
Just to prove we can do this with the alphabetical feature as well, I will now sort it by the [0] position of the keys:
{'Atlanta': 2780400, 'Boulder': 17098246, 'Detroit': 3619342323, 'Las Angeles': 9525067, 'Las Vegas': 99870, 'Paducah': 752612, 'Tampa': 9596961}
Merging is one of the tools that the Pandas module provides that sets it apart from just using the dictionary data structure. In order to merge dictionaries together, we do not necessarily need to write a function, as we can simply use the update function contained within the dict class. However, I like to write a function to do so in order to copy the dictionary rather than mutating the other, as I often focus on retaining the form of data when programming. That being said, it is a relatively simple operation that mostly involves calls on the functions that are already provided in the dictionary type. The first thing we are going to do to write this merge function is consider our arguments and output, we want to provide two dictionaries and get a single dictionary back. Let us put that into a function call:
def merge_dicts(one: dict, two: dict):
Now we will want to copy the dictionary so that we do not end up mutating the original type, because that could be quite catastrophic if we were to accidentally destroy the data in the first one — especially if we were to mess up this function on the first try. Since we are not working with the individual data-types contained within our dictionary, there is no need to do a deep copy, so we can get by pretty easily using a shallow copy.
temp: dict = one.copy()newdict: dict = one.copy()
Finally, we will call the update function and return our new dictionary:
newdict.update(two) return(newdict)
For a final result that looks like this:
def merge_dicts(one: dict, two: dict):    newdict = one.copy()    newdict.update(two)    return(newdict)
Now we will create a new dictionary to call this with:
additional_cities: dict = {    "Dover": 8323,    "Colorado Springs": 4990,    "Seattle": 95067,}
Now let us pass both of our dictionaries through our function!
allcities = merge_dicts(additional_cities, cities)
And this is the result:
{'Dover': 8323, 'Colorado Springs': 4990, 'Seattle': 95067, 'Detroit': 3619342323, 'Las Vegas': 99870, 'Las Angeles': 9525067, 'Boulder': 17098246, 'Atlanta': 2780400, 'Paducah': 752612, 'Tampa': 9596961}
Cool!
Dictionaries are often used to store data in Python. A lot of this data is iteratively added overtime, and sometimes there might be missing values or keys caught up in the mix than can create problems if we do not have a way to deal with them properly. For this problem, the solution is to use the setdefault() function that is included with the dictionary class. In the following example, I will do exactly that and default incoming keys without pairs to be assigned to the mean of our data:
allcities.setdefault("Unknown", sum(allcities.values()) / len(allcities.values()))
This will append the default to our dictionary while also making incoming data default to these two values.
As I mentioned before, the JSON data format is quite popular for sending, receiving, and even storing data for web-development and Data Science. However, likely one of the most popular formats for real data storage outside of a data base is Comma Separated Value, or CSV files. These files are great because they contain far less syntactical chars than a format like JSON. Using the CSV module, we can get a CSV reader that will translate our CSV files into data for us! Let us import it and try it out!:
import csv
Now we can create a new reader object by calling on the csv.DictReader() class with the open() method in order to get our CSV file into a string:
reader = csv.DictReader(open("spreader.csv", "r"))
Now we can iterate that reader type, providing us with keys and corresponding values:
for row in reader:    print(row){'hello': 'how', 'medium': 'is'}{'hello': 'everyone', 'medium': 'doing?'}
Finally, we can push these into a dictionary with the update() function that we used before:
for row in reader:    z[row["Code"]] = row
About a month ago, I wrote a really cool article on how you can actually visualize dictionaries in a tabular fashion just like you would a data frame. Since this was such a cool trick, I decided to add it at the end of this list so that dictionary conquerors could find a better way to look at their data. If you are interested in reading that article, you may check it out here:
towardsdatascience.com
The dictionary data structure is one that is nearly essential for software engineering work, and is even more crucial for Data Science work. The data structure makes it far easier to deal with multiple corresponding lists at once, and is unmatched in convenience and capability by the other data structures that are packaged with the language. I think that knowing how to use dictionaries effectively can set you apart from the crowd and create a significant impact on your work, and using some of these tips can certainly make the use of dictionaries a bit easier. Thank you very much for reading my article!