Join Barron Stone for an in-depth discussion in this video Add items to dictionaries, part of Programming Foundations: Real-World Examples.
- A moment ago, I wanted to call Amanda, to invite her over, but when I went to look up her phone number in my Rolodex, it wasn't there. I guess I never added it. Fortunately, Olivia has Amanda's phone number. - Here you go. - Thanks. Now I need to add a new card to my Rolodex for Amanda. I'll take a new Rolodex card, and write Amanda's name on the top, and then her phone number on the bottom. Five five five, nine seven five four.
Now we'll use my simple alphabetical hashing function to figure out where to put this card. Amanda starts with the letter A, so that's her index, and I'll store her card in the A section. Next time I need to look up Amanda's number, I can just flip to the A's, and find it right away. Adding Amanda to the Python rolodex is just as easy. I have the script start_08_02_rolodex from the exercise files open, which defines my original rolodex. Amanda is not currently in the rolodex dictionary.
I'll run the script and switch over to the interactive shell. Now, if I try to find Amanda in my rolodex, Python will throw an error, because it can't find that key in the dictionary. So I need to add her to the rolodex. To do that, I'll use the same command, but with an equals sign afterwards to assign her phone number, five five five, nine seven five four, as the value corresponding to the key Amanda. Now, if I try to access the rolodex again, to get Amanda's phone number, I find it right away.
Next time then. Okay, bye. Unfortunately, Amanda can't make it to the game night. But she told me about another friend of hers, named David, who loves board games, and might want to join us. I don't know this guy, but she gave me his number to give him a call. First, I should add him to my Rolodex. Just like before, I'll grab a blank card, and write his name at the top, David, and then put his number below that. Five five five, zero nine zero two. And then I'll add it to the Rolodex.
David belongs in the D section, so I'll go there, and uh oh. There's already a card for a David in the Rolodex. But these are different Davids. They have different phone numbers. Unfortunately, I can only have one entry per name in my Rolodex. If I allow my Rolodex to have multiple cards with the identical name David, then that would cause ambiguity later when I try to look up his phone number. There are just too many David's in my life. To add this new card to the Rolodex, I'm going to have to overwrite and remove the card for the previous David.
I'm back in the Python rolodex script that we looked at a moment ago. Since I already have a friend named David in my rolodex, if I try to access his entry, it'll return his phone number, 5558331. Now, to add Amanda's friend David to my dictionary, I'll use the name David as the key, and pass in his phone number that Amanda gave me for the value. Five five five, zero nine zero two. Now, when I try to look up David's number again, I only get one number, the number that belongs to Amanda's David.
I've overwritten, and lost, the original value for my friend David's phone number. Dictionaries are similar to sets. They do not allow duplicate keys. So, I have two different phone numbers that I want to store under the exact same key. Fortunately, the values in my dictionary can be more than just a single number. I can store pretty much any type of object that I want as a value. So, instead of assigning a single phone number to the key David, I'll create a tuple with both phone numbers.
Now when I try to look up David's number, I got the tuple back, with both numbers. Being able to store tuples, and other data types as the dictionary values, is useful because I can pack a lot of information into that entry for David. If I wanted to, I could make my rolodex really fancy by storing data structures that contain each person's phone number, address, email, birthday, etcetera. The possiblities are endless. But what I've done here, by just packing those two numbers into a tuple, is not really a practical solution for the rolodex, because I don't know which number belongs to which David.
But it's nice to know that I can store more than just numbers in a dictionary. For now, I'll reset the value to the original David's phone number. And what I need, is something to differentiate the keys for each David. I'll do that, by adding Amanda, in parentheses, to the string I'm using as the key for Amanda's friend David. Now I have both numbers stored in my rolodex, and I can tell them apart. My original friend David's number is stored under the key named David, as it originally was. And Amanda's friend David is filed under the name David (Amanda).
- Reusing functions
- Local vs. global variables
- Creating and naming custom objects
- Class inheritance
- Modules and packages
- Multidimensional lists and tuples
- Queues and stacks
- Creating and combining sets
- Storing data in dictionaries
- If/else and switch statements
- For vs. while loops
- Error handling
- Polling and event-driven programming