Level Up Your Julia Dictionaries. Use the dict datatype to its full... | by Emmett Boudreau | Towards Data Science

Dictionaries are one of the most useful datatypes at your disposal in not only Julia, but a host of similar languages like Python and Nimrod. Dictionaries are an especially valuable tool for Data Scientists, and this is illustrated even more severely once you realize that
DataFrames.JL’s DataFrames are just dictionaries.
Often dictionaries are overlooked and seen as relatively simple datatypes containing just arrays and keys, but I think this is a mistake. I think this is a mistake because Julia dictionaries are incredibly powerful and extremely versatile, being used effortlessly for a multitude of things from DataFrames to API returns.
notebook
One of Julia’s coolest features is the ability to use syntactical expressions. This is no different for a dictionary, as you can do some pretty cool things with a dictionary using syntactical expressions. This is really awesome for statistical computing because it can make an easy and better performing option available, with some pretty neat syntax. I personally like to use syntactical expressions for matrix multiplication:
det2(A) = A[1,1] * A[2,2] * A[3,3] + A[1,2] * A[2,3] * A[3,1] + A[1,3] * A[2,1] * A[3,2] - A[3,1] * A[2,2] * A[1,3] - A[3,2] * A[2,3] * A[1,1] - A[3,3] * A[2,1] * A[1,2]
Despite my love for matrices, I think that the ability to use this on a dictionary might even be more valuable than using this with a matrix. One great use I have found for this is inside of a graphing library I’m currently working on, where rather than using double iteration, I iterate using threads and dictionary indexes.
This makes it a lot faster.
To justify the use of this method, I will first present you with a scenario. Let’s say we just pulled a heap of data from an API using a get request, and we want to continuously pull said data whenever a script is ran, but won’t be monitoring said script. In this case, we would want to avoid trying to access keys that aren’t existent in the data at all costs, as that would terminate the script and result in an exception.
To mitigate this, we can use the get() method to create a default value for our script to fall back on in the case that the index we are searching for is not within the dictionary. The method takes 3 parameters; our dictionary, exampdict, our key, which is :a in this example, and our default value to fallback on in case that key is not found in our dictionary.
exampdict = Dict(:a => 5,:b => 7,:c => 8)a = get(exampdict, :a, 0)
As you can see, even though :d couldn’t be accessed because the data is not in the dictionary, we still got all of our values out accordingly, as well as a default value for d.
Using the in() method, we can much more quickly check for a key or a pair. This will return a boolean and takes two parameters, the key we would like to search for and the dictionary we would like to search in.
in((:b => 8), exampdict)
Thanks to Julia’s multiple dispatch, the filter() method can be used on dictionary keys as well as anywhere else. Using filter(), we can pretty much filter basically anything and everything out of our data frame that can be represented as a conditional. Here I combine to conditionals to check for keys that begin with the letter K that also have values below 4.
strdict = Dict("Orange" => 3, "Kiwi" => 2, "Kayak" => 5)filter(tuple -> startswith(first(tuple), "K") && last(tuple) < 4, collect(strdict))
Needless to say, filter() is certainly a very useful method! This method can be used for anything from only getting certain keys out of a dictionary to getting rid of missing values and values below a certain number.
Another handsome devil returning from Julia base is map(). Of course, map is used in various languages and has proven useful there as well. However, in Julia we can map both the keys and values of a dictionary, for example we could map all of the uppercase keys:
map(uppercase, collect(keys(dict)))
This can certainly be useful in many situations, but is certainly outdone by filter() in my opinion!
There are a lot of cool things you can do with dictionaries in Julia. In my opinion, they are really well thought out! One great thing about Julia is that you can use the same 10 standard functions from Julia’s base on nearly every type used in the language thanks to polymorphism, which makes learning methods and what they do incredibly easy in the language. Hopefully these tips are useful and come in handy, and maybe using these tips will even level up your Julia dictionaries!