1. Dictionaries
Next, we will look at dictionaries.
2. Dictionaries - introduction
A dictionary is one of the most common data structures. It can be thought of as a collection of keys and values. Each value has a key that allows accessing the value.
We can mix data types in a dictionary: keys and values can take on any data type.
3. Dictionaries - why are they useful?
So why use a dictionary?
Imagine we want to store information on a particular stock. We want the ticker, the closing price, and the volume.
We could use a vector to store each data point as an element. But when we want to access data, it gets tricky. If we want to get the ticker, we must remember that it is the first element. We cannot intuitively access the ticker, and with more data, we get overwhelmed.
Here, dictionaries are useful. We access values using keys, and we can call the keys anything. For our stock example, we can use the key "ticker" to store the ticker, the key "price" to store the price, and so on. We access the key in square brackets to get the ticker, just as with vectors.
4. Dictionaries - untyped
Let's look at the structure of a dictionary.
We use the Dict keyword to define a dictionary. We then write our key and value, separated by the equals and greater than sign, signifying the key pointing to the value. We separate key/value pairs with a comma.
We saw that we could mix datatypes in a dictionary, so the key and value can have different types. By default, a dictionary is untyped, meaning we are not forcing a key or value to have a certain type.
5. Dictionaries - typed
While dictionaries are, by default, untyped, it is possible to specify the type of the keys and values.
To convert an untyped dictionary to a typed dictionary, we specify the type in the dictionary declaration. Here, we are forcing the key to be a string while allowing the value to be any type.
Do you see any problems? If we try to pass the price as 131-point-86 from before, we will get an error, as it is not an integer but instead a float. Our key "AAPL" is also a string, not an Integer. Changing the type for the values to Any, we can see that the dictionary is now defined correctly.
6. Dictionaries - iteration
We can iterate over a dictionary similarly to any other data structure using a for-loop. Note that this returns each key/value pair together on a line, so we can see the ticker is AAPL, and the price is 131-point-86.
7. Dictionaries - iteration over keys and values
Two keywords - keys and values - return the keys and the values of a dictionary. Iterating over the keys and values allows us to access them individually.
8. Dictionaries - iteration via tuple unpacking
One final method of iterating over a dictionary is tuple unpacking, which will return each key/value.
9. Dictionaries - get()
To access values in a dictionary, we use the key. To do this, we use the get function. We pass in the dictionary, the key, and then a return value if that key does not exist.
If we don't specify a default value and the key is not found, we will get an error, so it is good practice to specify one.
10. Dictionaries - modification
The final aspect of dictionaries is modifying values. We can add new keys, modify values, and delete keys.
Imagine we want to add a new key to our dictionary, volume. We create a new key volume using square brackets and assign the value that we want to be associated with that key.
Printing the dictionary, we can see the new key called volume with a value.
We can use the same syntax to modify a value. Let's change the price to reflect an updated price.
11. Let's practice!
We've covered a lot, so let's practice!