Key-value pairs in Python are called dictionaries. A dictionary is similar to a hashed list or an associative array in other languages. Dictionary entries are typically accessed by their key.
- [Instructor] Python's dictionary type is a hashed key value structure. This is comparable to associative arrays in other languages. Here in Komodo I've opened a working copy of D-I-C-T, dictionary, .py from chapter eight of the exercise files and you notice that the dictionary is created using curly braces so there's the open curly brace and the closed curly brace and there's key value pairs. That's a key value pair.
Each of the key value pairs has a key on the left, a value on the right, and they're separated by a colon. The pairs are separated by commas within the structure sets. One key value pair, that's another one, and et cetera. You notice also that I have a little print dict function which prints the dictionary and when I run it you can see it prints it in a nice readable format. You can also create the dictionary using the dictionary constructor and keyword arguments and personally I find this a lot more convenient, and now it looks like this.
You can see this is a little bit easier to read and it's a lot easier to type and when we save and run it gives us exactly the same results. Keys and values may be any type. Keys must be immutable, strings and numbers can always be keys and this keyword argument method of creating the dictionary is obviously most convenient when you're using strings for your keys. The items method returns a view of key value pairs.
This can be used to simplify the loop. So I can say print... Or rather, for, we start with the for loop, for K comma V and so K and V are the key and value in animals.items. I print, we use an F string here and we'll have the key and the value like that and when I save and run this I'll just comment out this here and when I save and run it you see we get exactly the same result.
So I can easily replace this here and say O. This is a bit more readable. We can use that for a print dict. And I still get the same result. The keys method returns a view of dictionary keys so I can say for K in animals.keys print K and then we have to scroll up to be able to see that so why don't I just comment this out here and now we have a view of just the keys or I can just get the values like this with the values method and that'll give us just a list of the values.
A dictionary is indexed by its keys so you can easily pick a particular element, you can say print animals sub lion and that will just print the lion. It's just the value so the subscript returns a value just like it does with a list only in this case you're using the key instead of an index. In fact, you may use this to assign a different value to the lion, you can have it say, I am a lion.
And now when we print it we get lion is I am a lion. Or you may add a new item, say monkey. And monkeys of course say haha. And when I run this I now have monkey at the end of my list. You can search for a key by using the in operator. So say lion in animals and you'll notice that that returns a true value see that at the top of this there, true.
Or you can use that with any conditional expression. And so that'll say found and if I'm looking for something that's not there you'll see it says nope. If you try to access a key that doesn't exist you'll get a key error exception so if I say print animals sub Godzilla you notice that I get the exception of key error Godzilla because that key does not exist.
Or you can use the get method to return a value when you don't know if the key exists. I can instead say animals.get like that and I get the none value because that key doesn't exist so if you don't want the exception, you want just a none value, you can use the get method. So Python's dictionary type is both simple and useful. You'll see many examples in the rest of this course.
- Python anatomy
- Types and values
- Conditionals and operators
- Building loops
- Defining functions
- Python data structures: lists, tuples, sets, and more
- Creating classes
- Handling exceptions
- Working with strings
- File input/output (I/O)
- Creating modules
- Integrating a database with Python db-api