Long lists can be a good way to store a lot of data. Have a long list and you want to use a for loop to do something with every single element of that list. Then that's fine. But if there's one specific element or specific item in that list that you want to find, but you don't know the number or the index of that item, then it can be hard to access the pull out that one item There's another kind of way to organize collections of data in Python, called a dictionary. That makes it much easier to pull one specific item from your collection of data. We'll learn about that in this lesson, and we'll also see a fun example of using a dictionary to prioritize things on your to do this as they may be high versus medium versus low priority. Let's dive in and see how dictionaries work. Let's start of loading some helper functions as usual. And then I'm going to use the example of ice cream flavors and their descriptions. So, here's a longer list of ice cream flavors together with descriptions vanilla ice classic and creamy rich smooth flavor, and so on. The problem with using a list like this is, if you want to look up the description of a specific ice cream flavor, say mango ice cream and you don't remember what's the number or index associated with this, it's pretty hard to pull up this description of mango ice cream. So dictionaries in Python are inspired by real world dictionaries that people use to look at the meanings of words. So in a paper dictionary, maybe I'll have a word like aardvark and then the definition of that word, and then another word like say apple and then a description or definition of what is an apple. In Python, there is a similar mapping from words to definitions, except that we call them keys and values. So, for example, you might have a key which captures the concept of mint chocolate chip ice cream, and that maps to a description of that flavor. And a different key cookie dough may then map, in this case to the description of the cookie dough. Here's a Python snippet of code you would use to define a dictionary, where here, we're setting ice cream flavors to a dictionary where mint chocolate chip maps to description cookie dough maps oto this description. Let's walk through all the components of this code in detail. What I've highlighted in blue here, are the keys of the dictionary and highlights in orange here are the values and in a dictionary different keys, the ice cream flavors map to different values. In this case, the descriptions. Notice that keys and values are separated by colons, and here, ice cream flavors is the name of this dictionary that we're defining. Equal is a user assignment operator. And to define the dictionary you use curly braces to open and close, or to start and end the definition of the contents of the dictionary. And we also have commas separated out to different key-value pairs. The section here, which is why there's a comma there. So let's define the dictionary. I'm going to say ice cream flavours equals. Because I'm defining a dictionary I'm going to use a open curly brace. And for this example let's just use three flavors chocolate chip colon. Then the description of mint chocolate chip, then cookie dough colon description of that. And finally salted caramel. And then to close out the dictionary and we'll do close curly brace and then hit shift enter. And now I've defined ice cream flavors as my dictionary with three keys like these. And values. Let this text here on the right. And the different lines are, as before, separated by commas. So the points out again some key differences between dictionaries and lists. As you've seen, instead of using a square bracket we use curly braces. And in second, there are two items two strings per line instead of a single string per line. And these two strings are the key. This first string added a value, which is the second string and is separated by a colon. Because dictionaries have keys and values, we sometimes say that a dictionary in Python stores a set of key-value pairs, where each of these lines has a key-value pair. If you want to look at the keys, I can print ice cream flavors dot keys. Like so. And this prints out the three names as expected. And if you want to print out the values then we can run this. And this prints out dictionaries values. Now dictionaries might look a bit like this, but they behave differently. If you want to try to access the first item in this dictionary via, say, prints ice cream flavors zero, this will work because there is no such thing as the zero element. So let me hit shift enter and this generates an error message. So how do I access a single item in this dictionary? Let's try asking a chatbot. I'm gonna ask how to access the single item in this dictionary, and let's see what it says. And here it says cookie dough description equals ice cream flavors square bracket and then cookie dough and print cookie dough description. Let's give that a try. I'm going to copy this code and go back to my Jupyter notebook and paste that in. And let's run that. And there you go. It printed out the description for the vanilla ice cream. Loaded with chunks of chocolate chip cookie dough. So the code to access a specific item of the dictionary is to have the name of the dictionary ice cream flavors. And then these are square brackets. I know this could be a little bit confusing. When defining the dictionary, you use curly braces, not square brackets, but after you've created the dictionary, if you're accessing a specific item in the dictionary, you use square brackets and then here, is one of the keys and what the dictionary will do is look up if there's a key that says cookie dough, and if so, it then returns or passes back to description. That is the value associate of this key. And it stores that in the variable cookie dough description. And so we print this out. We call this description of cookie dough flavored ice cream. Let's look at how we can add an item to a dictionary. Let's add to this dictionary a description of rocky roads flavored chocolate. So here's my dictionary print ice cream flavors. And there's the chocolate chip cookie dough salted caramel. And if I want to add the description of Rocky Road, I would say ice cream flavors square bracket. Not curly braces, rocky road equals. And then, you know, here's the description. And if I now print out again ice cream flavors, is now expanded my dictionary to also have Rocky Road as a new key that maps to this description. Chocolate ice cream mixed with other ingredients. But wait with a few spelling errors. Here, and it turns out the same pattern can be used to update the value of a certain item in your dictionary as well. Where if I rerun this code chocolate ice cream mixed with other ingredients. Let's run that, and if I print ice cream flavors then now I get an updated description that has the errors fixed. Now, it turns out dictionaries can hold all sorts of data. In the example we use, we had ice cream flavors mapped to description of the ice cream flavor. But dictionaries can also map to numbers instead of to strings. So for example, if I want to create a dictionary of facts about Isabel, age 28, her favorite colors red, then I can construct a dictionary like this. And remember as a curly brace to build the dictionary. And here the key age maps to a colon, match the number of 28 and favorite color maps to another string. And if I print Isabel facts, I end up with this dictionary where one of the values is a number is an integer, and one of the values is a string. So dictionaries can map the keys to different types of data, in this case strings or numbers. Now, if Isabel has three cats, I was told the name of all of her cats. Well, one thing I might do is create a list. And so here's a list. I've created a list with square brackets, and in Charlie, Smokey, Tabitha as the name of her three cats. But how do I store the names of these cats? Well, one thing I could do is to add an extra key. Isabel. Facts. Square brackets, cat names equals this list of three cats. And so in this example, I'm taking the Isabel facts dictionary, creating a new key, cat names. And instead of cat names, mapping to a single string like red or a single number like 28, this key, cat names, now maps to a value where the value is itself a list of all three cat names. So let me do that and let's now print Isabel facts. And you see that this is a dictionary by age 28, favorite color master red, and cat names, maps to a list. And here's another example. I can set Isabel's favorite snacks to pineapple cake and candy. And if I then print Isabel facts after that, then it has a favorite snacks, pineapple cake and candy. Before we wrap up this video, I'd like to just go through one fun example of using a dictionary to help you keep track of and complete high-priority tasks. You previously saw an example of using a list to keep track of a set of tasks, and that was one fine way to do this. But let's level up our personal organization game, and let's take these tasks and store it in three lists that organize our tasks by priority. And so high-priority tasks is a variable that has stored a list of these two items. Medium-priority tasks is a variable. And so the list of these two items. And similarly for low priority tasks as a list with just a single item. Now let's create a dictionary to put these three lists inside of the dictionary. So I'm going to say prioritize tasks equals curly braces. And then I'm going to create a key high priority colon high-priority tasks comma. And so what we just did is treat a key that's the string high priority. And the value that this key maps to is high priority tasks which is this variable that we just defined up here. And similarly I'm going to create two new keys. Medium priority is a key that maps to this list of medium priority tasks that we defined up here. And same for the key low-priority close and braces. And let me now print out prioritize tasks. And now you see this is a dictionary where high priority is a string that maps to this list of two items. Medium priority is a string that, as a key, maps the value of this list of two items, and similarly for low priority. Now let's say you want to use a large language model to do the high priority tasks. Well, I can look up key high priority tasks by looking at prioritize tasks high priority like so. And I could actually print this out. And so this prints out the two tasks, compose a brief email and create an outline for a presentation. So prioritize tasks with the key high priority is a list of two items. And if you remember what we had seen in the previous lesson was a for loop. Let's see. To figure out what this might do and save for a task in and then prioritize task high priority. Remember, we need a colon in here and then indent four spaces. And then on the sprint and response task. Let's run this. Shift enter. And so this composes a brief email and then creates an outline for a presentation on remote work. And if you want, feel free to edit this code to have it then do your medium-priority tasks or your low-priority tasks for you as well. So you've learned a lot about dictionaries, and lists. In the next lesson, we'll go further with dictionaries and use the key-value pairs, specifically, the values of the dictionaries, to create customized prompts for a large language model. And we'll go through a fun example where we use a large language model to write custom food recipes for us. Let's go take a look at that in the next video.