5.2 Look at more data structures - Video Tutorials & Practice Problems
Video duration:
14m
Play a video:
<v ->So in lesson four we looked at the list to data structure.</v> And we stopped there because that is kinda like, the most accessible one, for new beginners to understand. Like a list of something, is self explanatory. And I wanted to show you now some more data structures, that come built in with Python that can be useful. And I'll show you them in order of, like similarity to lists. Okay so the first one we're gonna look at are Tuples. And they are similar to lists, because you can have multiple values, and they're ordered, and you can have duplicate values in them. But the main difference is that they cannot be mutated, and that's just a fancy word for they can't be changed. So you can't update them with new values, you can't remove values, you can't order them around. And this can be useful for certain things. But first before talking about that, I'll just show you how to make them. In Python, I can have my_tuple. And it's gonna just be using these parentheses, and then adding some values inside. So I can, access, adjust like a list accessing something at a certain, at a certain index. And this will give me at a zero index, I'll run it and it'll produce one. I can get two. Run that. And zero, one, two, it gives me three. Can also be minus one. So I can do all the same stuff as, what I did with lists. Like what you can access with lists. For example this will give me just two and three. But what happens when I try to mutate it? So print my tuple. And tuple object does not support item assignment. And let's just check that we can, actually put in duplicate values. Yes. So why would you use a tuple? Well say you have a function, and you wanna return more than one value In Python you can only return one object from a function. But it could be a tuple. And so it could have multiple values in it. And these can be different values. So the first one can be like the list, and then the second value could be false. Yeah, so you might not want anyone, to be able to change these values. Maybe just because you don't wanna make it look like, the function actually returned different values than it did. You just want people to be able to access them and use them. Next we're gonna look at Sets. And, comparing sets and lists. The similarities are they can be mutated, but some of the difference are, they don't maintain the order that you have set for them. And also they cannot have duplicate values. So let's look at creating Sets. My_set equals, and I can use these curly braces, and make a set, like this. And let's print it. And it deletes any duplicates, and only keeps them, it only has one instance of each. And then also say we had a four in here. It also orders them. So it doesn't maintain the order that you set for it. We can do various things. So I can add. So it would be like a pin but since there's no order, it's just called add. If I add one, then that's fine it doesn't complain, it just doesn't create a new item in the set. I can add 100 though and it will create a new item, and I can add, none, it'll put it there. And then false is equivalent to zero, so it'll actually put it up here. Let's see. What other Set functions are there? We can Remove an element, we can Copy it. Then there's also things like Difference, Intersections, Symmetric difference, Union. And these are all, functions for Sets if you know anything about Set Theory. So, we're not going to look too much into them. But maybe I'll say like I have another set. And, it's got the values of one, ten and 100. I can print my_set, and Union of my_other_set, oh my_another_set let's call that my_other_set. And now it'll combine them. Yeah and then if we need to look at Difference, then it's like this one minus this one. So it removes the ones. So that can be useful, for if you don't actually care about order, And you want everything, all the items to be unique. You can also, create a list. And then create a Set out of that list, by casting it into a Set. And it ends up doing the same thing. So sometimes you only wanna know, like what are the unique things in this list? Or in this word. And then you can create a set out of them. And then lastly we have Dictionaries. So Dictionaries are arguably like, the most important data structure, in Python like after lists. But they're more different, and they're maybe harder to wrap your head around, if you haven't seen them before. But in other languages you maybe have seen Maps or HashMaps. And in Python dictionaries are the same thing. Comparing dictionaries and lists in Python 3, they are also Ordered, and they can be mutated. In Python 2 they're unordered. And then other differences are, that each of the values are actually labeled. So you can access them not by the index, but by there label. And they can also not have duplicate keys. They can have duplicate values, but not duplicate keys. And that the keys would be the label. So lets look at what they are and how they're created. So, my dictionary, in Python it's always shortened to dict. I can create one using these curly braces again. But instead of having values like this, to create a Set, we have to have a key, and then colon and then a value. So this is what it would look like. Print it. And we can see that's what it looks like. We can access a value by passing in a Key. And so using this Accessor again with a square brackets, but now, we're not passing in an Index, we're passing in a Key. So if I got the value of one, or the key of one. It's gonna give me, this is the key, this is the value. If it was a list this would be zero and this would be one. So it would've given me this. So if I ask for three. It's going to give me 1,000. 'Kay that'll look for the key, and then give me the value associated with that key. And these keys don't have to be numbers. And actually the often aren't numbers. They're often strings. So it's called a Dictionary 'cause, you have like, apple as a string and then the value could be like, a round fruit. It would be like, looking in the dictionary for this word, and getting this definition. So banana, this definition could be like a long yellow fruit. This one could be kiwi, an oval hairy fruit. So now, I've got this error, because there is no key of three anymore, but there is a key of banana. And now it'll give me the definition, or the value of that key. So we can change things and add things. So to add things. It doesn't really make sense to, say add or append, because it doesn't know where to place it. So you actually just use the Accessor again, and just give it a new key. So orange, I can say a round orange fruit. And then it'll now be in my Dictionary. And it's there at the very end because I'm using Python 3. And it's ordered based on the order that I put things in. 'Kay if I, did apple, now it's changed this value. At this key it's now changed the value to be this one. So adding new values and updating, are using the same format. It just depends on if this key exists already. And, I can use the Del function, and it deletes the value at the key of apple. And, each of these values can be different. You can have like a list or, a Tuple as one of these values, or a list of a list of a list. But the keys have to be sort of like basic objects. So just numbers, or strings. But if I tried to put a list in here, then it gives me this type error, saying unhashable type list. So if you're curious about what unhashable type means, feel free to look it up. But it mostly just means that, like it has to be like a single object. Great and so what are some of the uses for Dictionaries? If you want to store data, it's kinda like giving them columns really, that you can access. So, for example you could do like, countries of the world and their capitals. And the country could be the Key, and the capital could be the value. There could be like, If you were trying to count the number of words, or if you were trying to count the instances, of each word in a document, then the Key could be that word, and the count could be the number of words, and then you could access, and then you could like update it, as you go along and find like a new instance of that word. So there's lots of uses for Dictionaries. It's really good to get used to using them. But don't worry if it's like a hard concepts, to get your head around at the beginning.