﻿Hello! Welcome to the Path to Programming; the audio supplementary materials for introduction to computer science! I am a former student that took this introductory course and today I want to talk to you about Dictionaries! Last week we talked about Lists which is a type of sequence and this week we will be talking about dictionaries which is a type of collection! All sequences are collections but not all collections are sequences. Dictionaries, like lists are used to store data but unlike lists you do not need an index to access the elements, instead you can access the elements using a key. Crucially, lists are ordered. It matters that one element comes before another element. Indices specify the position of each element in the list. The elements in a dictionary are stored in key and value pairs. While Python does store these key-value pairs in a particular order, conceptually we consider dictionaries to be unordered. You cannot use an index to access a dictionary. Like with sequences, you can use the in operator to check for an element. With a dictionary though, the in keyword checks if a key is in a dictionary. Now that we have talked about the differences between collections and sequences let’s get into the nuts and bolts of Dictionaries. A Python dictionary is a mutable collection that maps keys to values. This means that each value has a corresponding key which can be used to access those values. Because a dictionary is mutable, we can add, change and remove key-value pairs. An important detail to note about the mutability of Dictionaries is that only the values can be mutable (in case you don’t remember mutable means that you can change a value after creating it). The keys are NOT mutable. Imagine if you had a key to get into your car. If the key was changeable, then the key might not work with the car anymore. This is why it is important that the keys are immutable. We do not want to lose access to our values. To sum up, a dictionary consists of a collection of key-value pairs. Each key-value pair maps the key to its associated value. To create a dictionary you will use curly brackets and you will use colons to separate the values from their keys. You will first name the key and then use the colon and then you write the value. There are actually multiple ways to create a dictionary. The first is to name your dictionary and each key-value pair. In this case you name your dictionary, and use curly brackets and then give your first key its name, then use a colon and then write the corresponding value. You can keep adding comma-separated key-value pairs. Another way to initialize dictionaries is by using the dict keyword. Dict is a built-in function that allows you to enter a list of key and value pairs as tuples. Finally you can create an empty dictionary and then add pairs using an assignment statement. Simply write the name of the dictionary with the key in square brackets on the left hand side. Write the value on the right hand side. I would suggest that you try all 3 ways to initialize a dictionary. Depending on the type of problem and how many value key pairs you will need to have you will prefer different methods. My favorite method was definitely creating an empty dictionary and then using square brackets to name a key and give its associated values. Another important thing to note is that a dictionary value can be of any type. This means that a value can be a string, a number, a list or even another dictionary! Any of the data types we covered are fair game to use in your dictionary as a value type. Same goes for keys. They just can be mutable. So if we can’t use indexing how can we access the values? A value can be retrieved from a dictionary by specifying its corresponding key in square brackets. Because dictionaries are unordered, it does not matter in which order you input your data in a dictionary! You just need the key to access it. If you refer to a key that is not in the dictionary, Python will give you an error. This type of error is specifically called a key error, since the key you are searching for does not exist. One easy way to avoid this error from occurring is to use the in operator like you did in lists. Also as we mentioned dictionaries are mutable. You can change the value associated with a key and you can also add or remove key-value pairs. But you cannot change the keys! I know we are talking about a lot of concepts that apply to different parts of a dictionary, so an easy tool to help you are memory diagrams. As we mentioned in the previous weeks memory diagrams are helpful visual tools and if you are stuck on how the key value pair relationship works I would suggest drawing memory diagrams of dictionaries. I always mention how helpful memory diagrams were for me and this is just another instance of that. So how can you add and delete pairs from a dictionary? Or how can you change the value to an existing key? In order to add elements you can just use the assignment statement we mentioned where you write the name of the dictionary and use square brackets to name the key and then assign the value. To delete an element you can use the pop method, which is the same method you use with lists. If you want to overwrite the value for a key, you can use an assignment statement just like you did to add a new key-value pair. Simply use the same key and place the new value on the right-hand side. Python also has a method called update that allows you to update it. You can use the update method by putting in the name of the dictionary and then putting dot update and using normal brackets. This method is useful to know but you will not be using it that much. We have been talking about methods that mutate or change a dictionary after you have created it. I also want to mention some methods that do not mutate the dictionary but are very useful as they return elements in the dictionary. If you want to get all the keys, use the dot keys method which returns all the keys in that dictionary. Another method is dot values which returns all the values in the dictionary. These methods are extremely useful and I would urge you to memorize them as you will be using them throughout. Another useful method is the get method. The get method is another way to access the values in a dictionary but prevents raising a KeyError if the key does not exist. The get method is not too important for this class but can be handy. Finally the dot items method returns the key value pairs as tuples. The three method keys, values and items are really powerful and you will be using the latter two when you are iterating. The most limiting part of dictionaries can be the keys. If you enter a key that does not exist you will get a key error, so as we mentioned before, always check if a key exists before you extensively use that key in your code. Another limiting part of keys is that you can only have one key with a given name. Meaning that you can not have 2 different keys with the same name. Each key must have a unique name. Therefore duplicate keys are not allowed. This is because a dictionary maps each key to a corresponding value, so it doesn’t make sense to map a particular key more than once. Also, the keys of a dictionary must be of a type that is immutable, because as you recall keys are immutable. So you can’t have a key that is a list because they are mutable. This means a key has to be an immutable type like integer, float, string or boolean, most types we are used to using are immutable so this shouldn’t be a problem when you’re coding but just a detail that I want you to keep in mind! Up to now we have learned what a dictionary is, different ways to create a dictionary, how to use the key and value pair and why they are so powerful. The last thing you will be doing with dictionaries is to iterate over them, generally with a for loop. You can iterate through keys separately or values separately or the key value pairs together. When you are iterating through the keys in the dictionary you can iterate directly over the dictionary, you do not need to use the .keys method. An example snippet of code would be for key in my_dictionary. Again you don't need to use the .keys method. However if you are iterating through values then you will need to use the .values method. An example of snippet code for this would look like for value in my_dictionary.values(). The same is true when you are iterating through items (the key and value pairs), where you have to specify in your code that you are iterating through items by using the .items method. Here we are at the end of another episode! Today we talked about dictionaries, which is another very important data type in python, alongside lists. Lists and dictionaries are quite similar as they store a lot of data, but you access the data in them in very different ways.. Lists elements are accessed by indexing which is based on the order in which you enter the data, whereas dictionary elements are accessed by key that you associate with them, so they do not depend on the order in which you enter the data. This is one of the greatest advantages of using a dictionary. But because of these differences in some instances it might be better to use a dictionary or a list and you should familiarize yourself with both so you know which one to choose. We also talked about methods that are very important and useful for dictionaries such as pop, update, values, keys and items so make sure you also know all of these! Make sure you memorize their names and their purposes. Also make sure that you are comfortable creating dictionaries with the 3 different methods we mentioned. Thank you so much for listening this week! I hope you learned something new and I look forward to talking to you soon!