What is a dictionary in Python? In many ways, it's very similar to how an actual dictionary works. In a normal dictionary, to locate a word, you look it up by the first letter and then use the alphabetical ordering system to find its location. Likewise, Python dictionaries are optimized to retrieve values. You may remember how useful Python lists are to access an array of values. Dictionaries access values based on keys and not on index position. Therefore, they are faster and more flexible in operation. By the end of this video, you'll be able to explain the purpose and function of dictionaries in Python and identify the performance benefits of dictionaries. With a Python dictionary, a key is assigned to a specific value. This is called the key-value pair. The benefits of this method is that it's much faster than using traditional lists. To find an item in a list, you need to keep reviewing the list until you locate the item. But in a Python dictionary, you can go straight to the item you need by using its key. A dictionary is also immutable in that the values can be changed or updated. For example, you could declare the number 1 as the key, and coffee is the item, and then change these to any other number or drink item. But how does this work? How do you access or locate the item you need within a Python dictionary with the use of the keys? To demonstrate this, I'll access the coffee item within a Python dictionary. First, I declare my dictionary name as sample dictionary. Then an equals with a series of key-value pairings or keys and items in a pair of curly braces. I also make sure to separate each pairing with a comma. I then type the print function followed by the name of my dictionary. I need to access coffee item, which has been given a key of one. I insert the number one in square brackets. I run the print function and it returns coffee as a result, just as I intended. I can also update a dictionary by replacing one item with another. I just need to use the key to reference it while using the assignment operator equals to assign the value. For example, I can change Item 2 in my dictionary from tea to mint tea. I just write a new line of code that starts with the name of the dictionary, followed by the key I want to change in square brackets. Then I add an equals operator followed by the name of the new item. What the coding says is to take Item 2 in the sample dictionary and change it to mint tea. When I run this function, it changes the item. I can also delete an item from the dictionary. To do this, I write a line of code with the delete function followed by the name of my dictionary. Then I add the key for the item I want to delete in square brackets. In this instance, I want to delete Item 3, juice. When I run this delete function, it will remove the juice value from my dictionary. Finally, I can also use three different methods to iterate over the dictionary. I can use the standard iteration method, the items function or the values function. Let's explore these iteration methods and the other dictionary operations functions in more detail. To create my dictionary, I'll start by declaring a simple variable called my_d, and then use the assignment operator and then curly brackets. It can be the same as a set, but by default, that's classed as an empty dictionary. I can print that out by using a print statement, using the type function and then passing in the my_d variable, clicking on "Run". The class has actually come back as a type dictionary. Next, I'll add some values into the dictionary, and I need to do it in two parts. A dictionary holds what's called a key and a value. The key can be numeric, it can be string. But to signify the assignment, I use a colon and then put in whatever value that I want. In this case, I'll put in a simple string value of test. To signify this, I can change or have different keys, strings, integers or ints. I put in a string key of name, and then the value, Jim. I print out my dictionary using the print function. I now have a basic dictionary setup with a key of one name and then one being test name, being Jim. If I want to access a key in the dictionary, I just have to use the square brackets and then pass in the key-value. In this case of one I'm passing the numeric one. In the case of the string value, I just need to pass in the actual string value itself. Name, click on "Run", and I get back both test and Jim, which are the values for each corresponding key. If I want to add a new key into the dictionary or update it, I can simply do my d and then add a new assignment, two in this case, Test 2 click on "Run". The key is then added with the current dictionary. Update the key, I have to call out the values I want. I'm just going to update the first key, which is number 1, with a value of not a test instead of test. Click on "Play" and it's updated on the screen. The other thing to note about the dictionary is, if I try and put in a duplicate key, it doesn't allow this. If I put in the Number 1 and then not a test. Click on "Run" and the key will actually be overwritten with the latest one. Number 1 only appears once in the outputs and it doesn't allow the two keys to be printed out because it won't allow duplicate values to be set. If I want to delete a key from the dictionary I'll use the del operator, now I type my_d and then specify which key I want to delete. In this case, number 1 is then removed from the dictionary. With a dictionary, I can also iterate though. For example, I can use the for x in my dictionary and then print out the value of x. Click on "Run", and I get one. This only prints out the keys. In a lot of cases, I may need access to both. To do that, I use a method called my items. With that, I can then gain access to the assignment of both the key and the value. I do a printout here, key plus value. I'll use some concatenation to print out both the key and the value. Click on "Run". I have to be mindful because I'm using an integer with a string. I wrap that with the type str, click on "Run" again, and I get the value of the key and the value for each of the items in the dictionary itself. You should now understand the purpose and function of dictionaries in Python and their benefits in terms of performance.