Accessing Dictionary Items
- 01:43
How to access items in a dictionary in Python, by using keys to retrieve values.
Downloads
No associated resources to download.
Glossary
dictionary PythonTranscript
To access an item in a dictionary, first, define the dictionary by executing that cell and then simply type the name of the dictionary followed by an open square bracket, and then type the key that you want. When you execute that cell, you're going to get the value associated with that key. And if the value associated with that key is a list, you could simply add the index And that will give you the specific object within that list that you want to access.
Accessing an object in your dictionary like this is very similar to a Vlookup formula in Excel. So if I were trying to do the same thing, look for the key in a dictionary and get Excel to return to me the name of the company, I would write a Vlookup formula and I would refer to that ticker, which is the key.
And then I would give it the table array, which Python would already know because we are referring to the dictionary. The table array is essentially our dictionary equivalent in Excel, and then the column index number, which is like our index in our list within that dictionary. So let's say I want the name, I'd say the second one in Python, it's a zero index, so that would be one. And then I want it to be an exact match and I get the name of my company. So that's how that same process would work in Excel.