python dictionaries coding in simple English


If you want to know about Python dictionaries in simple English, then here’s a video for you. Okay today in programming in simple English, we’re going to look at dictionaries. So what’s a dictionary? It’s called an associative array in other programming languages. What does that mean? Well a dictionary has a value and you access that value with a key. So you have a key value pair. With a list the value is in a place so you can access it by the number. So the first element In the list you access by zero, the second by one, and so on. In a dictionary the value that you have in the dictionary is associated with a key. This is a bit like a hash table. So for example we have Monday Tuesday Wednesday Thursday and Friday How we can access them ? Well we’ve got a label or a key. So Monday’s key is one Wednesday’s keys is three, Thursday’s key is four. So if we want to access the value we can use the key. So how would we write this in Python? So here’s an example of the dictionary called weekday, and Notice the curly brackets We don’t really need to worry about the space. This is for example We have the key first then the colon, and then the value. Notice each pair is separated by comma, except for the last value doesn’t have a comma after. So that’s a dictionary. He’s another example. You don’t need to have a key as an integer, you can have a key as a string or any data type that you can use to access the value. So in this example we have year one is a string, and That’s the key and the value is 2018. Which is a date and in this example is a string. This study year is a dictionary. We have the key, and value. The key is year 1, year 2, year 3, year 4, and the values are 2018 19 2020 2021. So if we print that we can see this whole dictionary together. And you can see the pair of key value, key value. With the key first which we have the year then the number of the year, and the value is the actual year. So that’s a dictionary. How do we print that? Well you can just use a print statement as you see. How can we check our value is in the key? We can use ‘in’ the same as we would do in a list. So, for example, 5 in weekday is true. If we try to access something that hasn’t got a key so for example, weekday, 6, doesn’t exist, so that would be an error, because that key doesn’t exist. Normally if we want to access different elements within a dictionary then we would use a for loop. So in this example, we’ve got the for loop with weekday, and we’re gonna print the key. Which in the for loop is D and the value which is the name of the dictionary accessed by that key. In this example the letter D. And that will output each key and value as a pair as you see. To add items to a dictionary we’ll just get the dictionary name this example weekday with the key, a new key, and the value. You have to remember that if you write a key, if it already exists, you’ll be overwriting the one that already exists. So be careful when you add a new item into a dictionary. In this example, we’ve added six and seven, Saturday and Sunday, so now we have a full week inside a dictionary. If you want to change something you can just use the key and then change the value, so here we’ve changed Saturday and Sunday to ‘day off’ for both of them. When you create a dictionary you don’t need to create values inside it you can create an empty dictionary by using the curly brackets or the term dictionary d-i-c-t with round brackets. So that creates an empty dictionary. When we want to look through dictionary items we can use the term ‘items’. So here we have a for loop we want to access two things the key and the value so in here they call x and y And we’ve got weekday.items. So ‘.items’ returns the two parts of the dictionary the key and the value. So that is ‘.items()’ If you just want the key you can use .keys term. So that’s the same loop but just instead of .items its .keys. Again if we want the values we can use just use .values. So here we’ve got weekdays .keys put into a list. So if you just want to look at or collect all of the keys within a dictionary and put them in a list you can do this simply as the list name equals a dictionary named .keys And also if you want to do the same thing with a values you can use .values. Another thing that you may wish to do with the dictionaries is to sort them Surprisingly when you print your dictionary it doesn’t necessarily print it in any order. So you may have keys 1 2 3 4 5, if you print them it doesn’t necessarily print them in that order. The dictionary is not in any order. It’s not stored in an order, so if you want it in order you have to use this ‘sorted’ keyword. So here we have the same .items notation, the same for loop, but we have the word sorted to make sure the dictionary is printed in the order of the keys. So why might you use a dictionary? A common use, for example, is to count words. So here we have a long sentence “the cat sat in the tree, and then the cat sat in the garden”. That’s a string of words. If we want to create a list we can split that string by using the .split function into my list, which is a list. And then we create a dictionary called ‘mycount’ and what we want to do, we want to put each word inside the dictionary and count how many times that word occurs in the sentence. So we’ve got a for loop to go through the list, and then we’re going to check to see if that word is already in the dictionary. So we have a look at the first word. It’s not in the dictionary, so we go to the ‘else’ and then that word will be counted as one. Then we go through each word And if it’s not there, then the count is 1, and if it is there we add one to the count. So therefore we’re counting how many times a word occurs. Once we print that, we can see, here we have the list of each word and how many times they occurred. How many times they were in the sentence. So ‘cat’ was in it twice, ‘in’ was in it twice, ‘The’ was in it four times, ‘sat’ was in it twice, and ‘and’, ‘then’, ‘garden’ and ‘tree’ were in it once. So that’s a way how we can use a dictionary. To achieve the same thing there’s a function called ‘get’ So here we have the same for loop and instead of having to check to see if something exists already in the dictionary, we can just use the .get function. That function has a look to see if something exists in the dictionary already, and if it does it gives it a value which in this case is 0 and If not, it gives if it exists, it gives it the value it already has. So that way in our list if something doesn’t exist, here we give it a plus 1 value, so it’s going to be 1, and if it does exist already then we add 1 to the count. And at the end we print that and it has the same output. so the first example we looked through our dictionary and we had to check to see something existed or not. In a second example we use the get function and that did the same thing. Both pieces of code have the same output the same result. OK. Time for the quiz. Here we have a dictionary called messages. We have a key and a value of ‘a’ and ‘great’, ‘f’ and ‘fail’, and ‘d’ and ‘pass’. Write code that checks to see if ‘f’ is in the message dictionary. That’s question one. Question 2, print the message dictionary in order. Question three. Quite a lot of code here. What do you think it does? What’s the result of this code? So you can think what does it do? Or you can think of what is the output? Briefly, we have a dictionary called ‘newdict’. We have A, B and C, which are all dictionaries. We have a for loop where we go through A, B, and C, and then we have a .update that does something to this new dictionary. And then at the end it prints it, so what do you think that does. That is question three. Question four. Create a dictionary from the following string with each letter as a key, and how often that letter occurs as the value? So we have a string ‘mississippi’. So, for example, the letter ‘m’ will occur once, or does occur once. So the key would be ‘m’ and the value would be 1. Whereas the letter ‘i’ occurs four times, so the key would be ‘i’ and the value will be four. Question 4 do not use the get function, so code that without using the get function. Question 5 is the same as question 4, but this time use the get function. So create the dictionary, count the letters, using the get function. That was question 5. Okay, let’s go through the quiz answers. Question 1. Check to see if ‘f’, the key ‘f’, is in the message dictionary. It’s quite straightforward, we just use ‘f’ in messages. So ‘f’ in the dictionary name and the output would be true, so that checks it exists. So that was very easy. That was question 1. Question 2. Print this message in order, so we have a key value pair in this example. We’ve used .items. The main point is we use the word ‘sorted’. So this would print in key order, so the keys are A, D and F. They’ll print them, the key values, in that order. So that was question 2. Question 3 we had this code and asked what would be the result? So what what would happen if we run this code? Well you may have guessed it adds all the values all the key value pairs inside all of the dictionaries and puts them together. So therefore there was five different parts of key values in the three dictionaries, and now they’re all in the one dictionary. That was question three. Question four we wanted to create a dictionary that counted letters The first example question four, we want to count the letters without using the get function. So here’s the code. We’ve created the dictionary ‘mydict’, and then we have the string, we’ve got a for loop to go through each letter then if that letter already exists. Then we add one to the count. If it does, the count is going to equal one, so the value that’s got the key, which is gonna be the letter, will then equal one. If it does exist then we’re gonna add one to the count. So that’s the output. We have each letter ‘i’, ‘p’, ‘s’ and ‘m’, and how many times they occur in the word, ‘Mississippi’. So both letters, ‘i’ and ‘s’ occur four times, ‘m’ occurs once and ‘p’ occurs twice. Question five was the same as question four but this time we use needed to use the get function. So similar code to start off with, the dictionary, and the string, and the for loop, but instead of having to check we can just use the .get function. So here we have .get we’ve got the variable in the for loop which is ‘l-e-t’ and here we either start off at zero and add 1 to make it 1 or we get the value and add one to it. So we increase the value by 1 if it exists. So as you might expect if we print that we have exactly the same output. Okay, well that was quite straightforward, but if you didn’t get all the answers correct, was this something that you found confusing? If so, then let us know. Okay. Please subscribe. Now the code review. Thanks for watching.

One Reply to “python dictionaries coding in simple English”

Leave a Reply

Your email address will not be published. Required fields are marked *