Python for Absolute Beginners Transcripts
Chapter: Cleaner code with common data structures
Lecture: Concept: Common data structures and when to use them

Login or purchase this course to watch this video and the rest of the course contents.
0:00 We've discussed three important data structures that you see all the time in Python. Dictionaries, lists and sets.
0:08 Let's talk about when to use which one that can be really challenging. So, let's start with our friend, the dictionary
0:14 the one we've been using the most. When is it a good idea to use a dictionary? If you're going to look up an item or value by key.
0:22 If I maybe have a user ID and I want to get the user object. Or I have a player and I want to get their score.
0:29 Or I have a role and I want to get a complicated data structure that represents the outcomes when playing against just that role.
0:37 So this is really the use case for dictionaries I'm going to look up something up by some kind of value and this lookup is extraordinarily fast.
0:43 It basically doesn't matter how large or how many items are in the dictionary it's able to look it up super, super fast.
0:49 Dictionaries are not good choices when you need serial access, you want to preserve the order
0:54 you want to sort them, or you want to access them by index. Give me the third item in a dictionary is not really a question that you can answer.
1:01 Until recently, dictionaries didn't even preserve their order and sorting them also doesn't make sense. So, that's when you don't use them.
1:09 Now, they look like this down the bottom. We saw there's many ways to create them and work with them and so on, but you print them out
1:16 they're going to look something like this. Key:value,key:value. The other really, really important data structure is the list.
1:24 This is good when you have serial access I want to access it one, and then the other, then the other. I want to preserve the order or I want to access
1:32 something by position. Give me the third one of those or I want to have a bunch of items and I want to sort them.
1:38 It's bad for basically when you're going to use something like a dictionary, looking up something by key or a special value. I've got a list of users.
1:47 If I want to go through it and find the user with name such and such, I can do that, but it's very slow
1:52 and the larger the list gets, the slower it's going to be whereas the dictionary doesn't have that problem. And it looks like this at the bottom.
1:59 Square brackets, individual items separated by commas. The other major one we touched on was sets.
2:06 The most important idea of a set is that it is a distinct collection of items without repetition. It also is really good at set operations.
2:14 If I have two sets, I can subtract one from the other and it'll show me just what's in the first one
2:19 after taking out the duplication from the second one. It intersects in all sorts of, you know set based operations you might do.
2:27 Just like dictionaries it's extremely fast to ask Is this thing in the set? It's bad for serial access, it's bad for preserving order
2:36 or sorting or accessing by index. None of those things can you really do. Also, not even knowing how many items are there, right?
2:43 If I have a set of three things well maybe it started out with five but it had duplications, so it reduced down to three.
2:49 So, that's also not so good there. But it does have its own special uses. Other data structures you might hear about are trees, linked lists, arrays
2:58 which are similar to lists, but usually they have the same data type and they can't change shape like lists have more items added after it's created.
3:06 And also, graphs. Graphs are really interesting data structures. It's unlikely you're going to mess with any of those.
3:12 But it is quite likely dictionaries and lists will appear in your day-to-day work.


Talk Python's Mastodon Michael Kennedy's Mastodon