Python Part 11 - Dictionaries Office

Python Part 11 - Dictionaries Office So welcome to this wisely tutorial on working with dictionaries in python here's what you'll learn during this tutorial we'll begin by looking at what.

Dictionaries actually are which always used to puzzle me a bit i'll be honest we'll then look at how you can create a dictionary in python and then how you can loop over the items in it to list.

Them out then we'll look at how you can edit the dictionary by adding and removing and editing items and then we've gone to a worked example.

A realistic example we're going to store a list of films in the dictionary and then in the second part of the example i'll show you how to sort the films and print them back out.

Again as always you'll be able to click on the link at the top right of the screen at any time which should appear about now to download files to do with the.

Tutorial and any exercises and as always i'm now going to vanish so that i can hand over control to my colleague sven who will reliably guide you through the rest of this tutorial.

Python Part 11 - Dictionaries

So let's get started what are dictionaries well basically they're set of key value pairs but that doesn't really quite answer the.

Question because we need to find out what a key value pair is take the example of an online dictionary and suppose you want to know what an owl means.

You type in the word owl in the cambridge dictionary for example and it gives you a definition of it in fact it gives you a couple with some pictures this is a key value pair.

The key is the word owl that's what you use to look up items you could have used any word you like and you would have got different definitions.

And the value is everything else so it's all the definitions and the pictures and the grammar grammatical notes you get as a result of looking up a word so that's called the value.

So let's look at a couple of realistic examples of how you might use a dictionary for the first one let's assume that you have a national insurance database.

    You give your national insurance number

    And i really really hope at this point that this national insurance number doesn't belong to anyone and if it does i do apologize.

    So you give somebody your national insurance number and the dictionary gives you back their name and details so if you wanted to set this in python.

    You would type in something like national insurance square brackets and then the key value and you'll get back in this case a tuple giving three bits of information about.

    That person their first name their last name and their date of birth so let's do a second example of a dictionary supposing that you wanted to set up a.

    Dictionary of all the weizel python tutorials there's 10 of them so far although this is actually the 11th the key in that case might be the number of the tutorial 10.

    And the value might be the name of the tutorial the previous one was called sets and how long it lasted so in this case if you wanted to set that in python.

    You could type in on the left hand side the name of the dictionary let's say i was called wisel tutorials the key you want to retrieve which is tutorial number 10.

    And the value in this case would again be a tuple given the name of the tutorial its length in minutes and in seconds so that is what a dictionary is.

    What we're now going to do is look at how you create dictionaries in python before going on to some examples of actually using them you'll see that i've created a file.

    Called creatingdictionaries.py

    And what i'm going to do is show you two ways to create a dictionary of all the first five possibly even just the first three tutorials in this series very.

    Self-referential so i put the names of them up above just so i can refer to them and so you can see where i'm going so the first method of creating a.

    Dictionary which i'm going to call method one for fairly obvious reasons you can create the name of a variable so i'm going to call my vids1 for equally obvious reasons.

    And what i can do is use the curly brackets to create a dictionary now if you've been watching this tutorial series to date the last thing you'll have done is create a set and i.

    Taught you in that tutorial that when you put curly brackets in you're creating a set well it's all down to context python will determine from what you write.

    Inside the curly brackets what you're trying to do so the syntax we're going to use is we're going to put the key which is how we look up the value in the dictionary.

    Then a colon and then the value so i'll just call my first one setup i'll call my second one vs code because i think the last thing you want to do is sit and watch me typing in.

    Information and i'll call the third one ranges etc and that will create a dictionary do you believe me you shouldn't so let's print it out so.

    I'll print out two things i'll print out the variable itself but then i'll print out the type of the variable always important to check you've got the right sort of thing.

    Now when i run that you should see it gives me the dictionary but also tells me that it is a dictionary or a dict as it's called for short so that is my first method for creating.

    A dictionary the second method i'm going to create another variable called vids2 and this time i'm going to use the dict.

    Function now because it's a function i put round brackets in and then what i can do within the round brackets is specify my key value pairs and the syntax is this you put the name.

    Of the key an equal sign and the value but it doesn't look like it's happy with that and the reason is the key name in this.

    Case if you use this syntax can't be a number as far as i can see so what i'll have to do is put something like v1 in instead and then is happy with it.

    So for the second one i'll put v2 equals and i've got this vs code and finally i'll do v3 and set that equal to ranges etc marks.

    So i'll do the same thing with this i'll print out the dictionary and it's type making sure that i switch from the first dictionary to the second.

    One and when i run this code you should see i get exactly the same thing for that the only difference is i've used a different key the first one uses an.

    Integer key the second one uses a text one which method's better well aside from the obvious fact the second one didn't allow us to use integers i think it's.

    Really up to you i quite like the fact that the first one is implicitly creating a dictionary without you having to declare that which seems to me a bit more pythonic so i'm.

    Probably going to go with the first one if we come to this in the future so that's how you create dictionaries now let's see how you can loop over the values to get them back out again.

    So just before we begin i'm just going to comment out these lines of code so they don't get in the way and i'll also rename my variable from vids1 to vid to make it easier to type.

    So to print out or loop over the values in a dictionary so to loop over the dictionary elements it's quite nice and intuitive up to a point.

    So we saw earlier the new enumerate function for looping over the items in a list which allows you to get at their individual index numbers and the syntax for this is very similar in that you put.

    Two variables instead of just one in the four clause i'm going to call mine key and value at least the first time around i'll do two versions of this.

    And then you can put the name the dictionary and that's what it should be and that would be nice and intuitive so then i could print out the.

    Information so i could print out a message containing a placeholder for the video number and then a placeholder.

    For the video name format functioning to fill in my bits of information which are the key and the value.

    But if i try running that you'll see it gives me an error message it says cannot unpack non-integrable int object and the reason for that is for some.

    Reason python won't let you just put the name the dictionary this seems to me a failing in python you get used to such high standards that it's disappointing when it lets you down.

    DISCLAIMER: In this description contains affiliate links, which means that if you click on one of the product links, I'll receive a small commission. This helps support the channel and allows us to continue to make videos like this. All Content Responsibility lies with the Channel Producer. For Download, see The Author's channel. The content of this Post was transcribed from the Channel: https://www.youtube.com/watch?v=qfawjXRck9U
Previous Post Next Post