Dictionary Workout
- 04:32
How to create a dictionary in Python using data from a table.
Downloads
No associated resources to download.
Glossary
dictionary PythonTranscript
What you're going to do is you're gonna take the data in this data table and put it into a dictionary in Python, in the form of a key that is the company ticker in, in a string format, and then a list containing the current price and the first year price target. And once you get that all into a dictionary, you're going to print out one of the lists and you're gonna print out a specific object within one of the lists. I'm gonna show you this data again right now. We're gonna go, give you a little more thorough explanation of what you need to do in this exercise.
In your Jupyter Notebook complete the following steps. First, you're gonna define six lists using the data table I just showed you, and you're gonna see it again at the end of this explanation. Use the company tickers for variable names. And remember when you're defining a variable, this is not a string. You use plain text followed by the equal sign, and then whatever you want to be in that variable, which in this case is a list. The first object in each list should be the company's current price, which you're gonna see again at the data table. I'm gonna show you after this explanation. And the second object in each list should be the price target. After you create those six lists, you're gonna create a dictionary called Price Dict, and that dictionary is going to contain six different items or six different key value pairs. The key for each item should be a string of the company's ticker. And the value for each item should be the list that you defined in step one. Remember, each key value pair is separated by a colon, and each key value pair must be followed by a comma before you type in the next key value pair. If you don't remember the colon and the comma, the dictionary will not work. So your key is gonna be the string of the company's ticker, and the value is just gonna be the name of the variable you defined with those six lists. Once you have your list created and your dictionary defined, you're going to print out the entire value for PFE, which is Pfizer, and then you're gonna print out only the price target for Amgen, AMGN. And you're gonna do that by referring to the index of the list in the value. Again, here's the data that you're gonna need to create your list and your dictionary.
I've gone ahead and completed step number one, which was completing the six lists for each of these companies containing their current price and their price target. Let's put those values into a dictionary.
I'm gonna start by opening with a curly brace and then hitting intern. When I do that, it's gonna automatically indent, in my key for each of these is going to be a string of the ticker, and then I put a colon, and then the value that that matches that key. And in this case, it's just gonna be the list that I defined as J and J, that and then a comma, and then I move on to the next key value pair. The next item in that dictionary, which in this case is UNH and so on. And for the last item, you do not need a comm after it because there's nothing coming behind it. Now that that dictionary is defined, I can print the entire value for Pfizer by typing the name of the dictionary, and then in square brackets the key. And then I can print only the price target for Amgen by typing the name of my dictionary in square brackets the key. And that alone would give me the value. I know the value that's attached to that key is this list right here. So then if I add the index for the value that I want, which is the price target, so that's in the second position index one, it's gonna give me this 205 right here. So I'm gonna execute that cell.
And I've made a critical mistake here, which is I've forgotten to use the print function. And if you forget the print function, then it's only going to give you the last line. So let's add those.
I now, when I print it, I can see I'm getting this Pfizer list right here, and that's flowing into this first line of my output. And then I'm getting that 205 price target for Amgen.