Locked lesson.
About this lesson
What is a dictionary and how do we use them?
Exercise files
Download this lesson’s related exercise files.
Dictionaries.docx59 KB Dictionaries - Solution.docx
59 KB
Quick reference
Dictionaries
A dictionary is a key/value paired list.
When to use
Use them when you have a list that's not numbered in nature.
Instructions
To create a dictionary:
fav_pizza = {
"John": "Pepperoni",
"Tim": "Mushroom",
"Mary": "Cheese"
}
To call an item from your dictionary, reference the key:
print(fav_pizza["John"])
Hints & tips
- Dictionaries contain key/value pairs
- Dictionaries are not numbered like lists
- 00:04 In this video, I want to look at dictionaries and
- 00:06 dictionaries are the last of the five data types that we're going to be looking at.
- 00:10 And a dictionary is very similar to a list and a tuple.
- 00:14 But with a list and a tuple, the items are indexed by number, you know zero, one,
- 00:18 two, three, four, whatever.
- 00:20 With a dictionary, their indexed with a key value pair.
- 00:24 So instead of zero, we use a key, and we have to designate what those keys are.
- 00:29 So let's create a quick dictionary, and
- 00:31 let's call it fav_pizza, we've done this before.
- 00:34 Now, to create a dictionary, we use these,
- 00:37 not the square brackets, not the parentheses, but the squiggly brackets.
- 00:41 And you can put all of these items on one line, but
- 00:44 the convention is to sort of do it on multiple lines like this.
- 00:47 So like I said, a dictionary has a key value pair.
- 00:50 So the key, let's call name and we put a colon, and then the value.
- 00:55 John's favorite pizza is pepperoni.
- 00:58 And you separate these key value pairs just like a list or a tuple with a comma.
- 01:03 And like I said, you could put the next one on the same line but the convention is
- 01:07 to put each of them on separate lines so that it's easier to read this way.
- 01:11 So now we have Tim, Tim likes mushroom pizza and
- 01:16 we have Mary who likes cheese pizza.
- 01:19 And you see the last item,
- 01:20 we don't need to put a comma behind it because it's the last item.
- 01:23 So, just like everything else we've look at, we can print these out just like that.
- 01:28 And that'll print the whole thing squiggly brackets and everything.
- 01:32 To access a specific item in our dictionary, again,
- 01:35 we use these square brackets and we just call the key.
- 01:38 So the first item is the key value, key value, key value.
- 01:44 So if we want John's favorite pizza, all we have to do is type in John.
- 01:48 We save this and run it.
- 01:49 Boom, we get pepperoni.
- 01:51 So super easy.
- 01:52 The main difference between this and the list is with a list,
- 01:55 you need to know the number, the zero, one, two, three.
- 01:58 With a dictionary, you need know the key, so John, Tim, Mary.
- 02:01 So here's an interesting sort of thing.
- 02:03 These keys are immutable.
- 02:04 They can't be changed and you can't use another one twice.
- 02:08 So let's say John supreme pizza, everything, right?
- 02:12 So if we save this, now if we call John, what's going to happen?
- 02:14 We have two Johns.
- 02:16 Well, see right here, it's already showing an error.
- 02:19 But this, well, actually we need a comma here, that's why we're getting an error.
- 02:23 There we go. But we get this error up here at the top,
- 02:25 duplicate key John.
- 02:27 So what happens? Well, actually, this will work.
- 02:29 But what will happen is the first one will be ignored, and
- 02:32 whatever the last one listed, that'll be the one that gets used.
- 02:35 So that's not a great thing, we really don't want to do that.
- 02:39 Just something to keep in mind.
- 02:40 So just like lists and tuples, we can add items, we can remove items.
- 02:44 If we wanted to get rid of John, we could just use our delete function and
- 02:49 then call favourite pizza and then just pass in the item we want to delete.
- 02:54 So John, so now, we just print out our dictionary.
- 02:59 Now all we have is Tim and Mary, because John has been removed.
- 03:03 We can also update an item by saying favorite pizza and
- 03:07 then setting equal to, let's say Onion.
- 03:10 So, now, if we print this out, we get Tim, Mushroom, John, Onion, Mary,
- 03:15 Cheese, and the order has been swapped around.
- 03:18 Which doesn't really matter because these are in numerical order, anyway.
- 03:22 If we were to call out John,
- 03:23 we don't really care what position John is in inside of our dictionary.
- 03:28 We're still going to get Onion, that's been updated.
- 03:31 So finally, we can add an item to our dictionary.
- 03:35 Say we want to add Bob in here, all we have to do is call our dictionary name,
- 03:39 fav_pizza and then slap a .update on here.
- 03:43 And then pass in whatever we want.
- 03:46 So let's go, Bob and Bob likes, we'll give Bob ham.
- 03:52 So now if we save this, and print, and run it again, we get Tim, Bob, John, and Mary.
- 03:57 Bob likes ham, there we go.
- 03:59 And obviously we can then, in the future, call Bob and just get ham.
- 04:04 So these key value pairs, the key can be a string,
- 04:08 it can be a number, I believe it can be a tuple as well.
- 04:11 I'm not exactly sure why you want to do that, but you could do that.
- 04:13 So we could change John to 1, well, no, let's go 41.
- 04:18 So now if we just called 41.
- 04:21 Let's get rid of that.
- 04:23 We get pepperoni.
- 04:24 So it works the same way if you're using a number, or
- 04:28 if you're using an actual string.
- 04:30 I don't know why you would use a number.
- 04:32 If you're going to use a number, you might as well just use a list or tuple, right,
- 04:35 because those have numbers built in.
- 04:37 So anyway, those are dictionaries, and
- 04:39 that is now we've completed our list of all the main data types.
- 04:43 In the next video, we're going to look at data type conversion,
- 04:46 how to convert from one type of data to another.
- 04:49 So that'll be in the next video.
Lesson notes are only available for subscribers.