1.11: Dictionaries - nealtran1905/PythonForResearch GitHub Wiki

Dictionaries are mappings from key objects to value objects. Dictionaries consists of Key:Value pairs, where the keys must be immutable and the values can be anything.

# Dictionaries themselves are mutable so this

means once you create your dictionary, you can modify its contents on the fly. Dictionaries can be used for performing very fast look-ups on unordered data.

A key aspect to be aware about regarding dictionaries

is that they are not sequences, and therefore do not

maintain any type of left-right order. This means that if you're looping over a dictionary, the Key:Value pairs will be iterated over in arbitrary order. Let's look at a diagram to clarify this idea. We're going to set up a simple dictionary where we have our first key that's associated with a value object. We have our second key that goes with another object. And let's say we have key number four here which goes with the corresponding value object. If this is a dictionary, this key object will always be associated with this value object. Similarly, this key will always go with this value object. When we say that dictionaries don't maintain any type of left or right order, what we're saying is that the ordering of these key-value pairs themselves is not defined. That means if I'm looping over these pairs, I might first get the pair that corresponds to my second key here. Let's then look at some uses of dictionaries. I would like to set up a dictionary which is called age. And if I want this to be an empty dictionary, I have two ways to construct that. The first approach is just to use a pair of curly braces, and this would give me a dictionary age which would be empty at the time of construction. Another possibility is to type "age = dict", which is a key word, open and close parentheses. And again I have created an empty dictionary. Let's then construct a dictionary that consists of names and ages. In this particular dictionary, the names are going to be the keys and the ages are going to be the value objects. Imagine the first task we'd like to do is look up a person's age. We do this using the following syntax. We first type the name of the dictionary. We then follow with the square brackets. Inside the square brackets, we insert the key we're interested in looking up. Let's say, in this case, we'd like to know, how old is Pam? So we'd enter "Pam" inside the square brackets. And Python returns the result to us. Often, we'd like to modify the value objects that are associated with specific keys. Let's say we would like to increase the age of Tim by one year. Here is how we could do this. We first take our age dictionary. We'd like to modify Tim. And let's say we want to increase his age by one year. We could use the following kind of syntax. We could say age of Tim is equal to age of Tim plus 1. And this certainly works. However incrementing the value of an object by one, or by some other number, is a very common operation. And there's a very handy shorthand notation for doing that in Python. So let's take the age of Tim, and say we would like to increase that by one year, we can do the following: We can just type "+= 1". And what happens if we check the age of Tim, we will have increased Tim age again by one year. Let's make sure we understand what happens when we say plus equals. When you're reading code from left to right, the plus operation happens first. Then we have the equal sign, which means assignment. So when we say something plus equals 1, for example, we first take the value of the object, we add one to that object, and then we reassign it back to the original object. A common mistake is sometimes to mix up the order of these two operations. Think about what happens if you say x equals plus 1. You're not incrementing the value of x by 1. Instead, you're saying that x should be assigned the value of plus 1, which is just the same thing as saying x is equal to 1. If you wish to increment the value of a variable by 1, you first do it the incrementation and then the assignment. That's why the order of these two operations is plus and then equals. You can use the dictionary method keys to find out what are all the keys in the dictionary. And similarly, you can use the values method to find out what are all of the values in the dictionary. When you call these methods, Python returns to you an object which has a very special type. The type of the returned object is what's called a "view object". View objects do precisely what you might imagine that they do. They provide a dynamic view of the keys or values in the dictionary. A key point here is that as you update or modify your dictionary, the views will also change correspondingly. Let's now work with the dictionary that we just defined. We're going to first extract all of the names in the dictionary. So I'll do that by saying "names = age.keys". Now let's just check the type of the object names. And Python confirms that this is a dictionary keys object. Let's say I would like to add a new key to my dictionary. Let's add a person called Tom who is 50 in our dictionary. So we say age["Tom"] and we assign the value object 50 to that key. If we now ask, what are the names, you'll see that the view object also contains Tom. We didn't redefine the content of names, and this is the nature of view object. As we modify the dictionary, the content of the view object will change automatically. Let's look at the values method. So if we say age.values, Python returns to us all of the values in the dictionary. I'm going to call this ages, and we can look at that object. So in this case, we have one, two, three, four, five different ages contained in the dictionary. I'm going to add one more person to the dictionary. Let's call that person Nick, and let's say Nick is 31 years old. If I now ask Python, what are the names, you'll see that the name Nick is also contained in the names object. This is the nature of views objects-- their content is dynamically updated as you modify your dictionary. Finally, let's see how we'll test for membership in a dictionary. We know that we have a key Tom in our dictionary. If I want to ask if Tom is a member of our dictionary, if Tom is a key in our dictionary, I can ask, "Tom" in age? And the answer is going to be "True". If I ask, is "Zofia" in our dictionary? Python is going to say "False". This is how we test for object membership in a dictionary.