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