Join Michele Vallisneri for an in-depth discussion in this video Using Python dictionaries, part of Introduction to Data Analysis with Python.
- View Offline
- [Voiceover] While lists associate numerical indices, starting at zero with an order sequence of elements, dictionaries map names to values. Here, names is actually a little more general than strings. Numbers work just fine and, in general, any Python object that is hashable may be used as a name. Hashable means that Python can convert it to a number. That's true for many types of objects. What we're going to review in this movie is how to create Python dictionaries, known as dicts, how to look at the individual elements, how to merge dictionaries, and how to loop over dictionaries in a few different ways.
We will now open the exercise file for this movie. To do that, if you're already in the IPython notebook, we can go back to its Finder interface and click on the 02_03_dicts_begin file. If your not in the IPython notebook, you would have, first, to open it up and then navigate to the location in the file system where you can find your exercise files. So let's talk about dictionaries. In Python, dictionaries are denoted by curly braces, again, with items separated by commas.
Each item consists of a key such as a state or a country and a value, in this case, the name of its capital Let's write a few more. Just as we do with lists, values are accessed with a bracket notation, but instead of a number, we're going to use a key. For instance, we may wish to look at the capital of Italy. This same notation can be used to add items to a dictionary.
Let's look at our dictionary again. Access in a non-existent item results in a key error. We can also check whether an item exists or not with the in operator. So Germany is not a key in our dictionary, but Italy is. Combining two dictionaries is a little more inconvenient than combining two lists. This is because the sum of two lists is simply the concatenation, but with dictionaries, we may have collisions between items.
So the way to think about combining two dictionaries is that we have an existing dictionary and then we update it with a new one. If there are new values for the same keys, those will be replaced in the resulting dictionary. For instance, let's create a second dict of more capitals, including Germany this time, and let's update our existing capitals dictionary with more capitals. Well done. Again, we can delete items in a dictionary using del and identifying them by their key.
Looping over a dictionary is very similar to looping over a list, however, there are three different kinds of loops you may want to do. The most straight forward's syntax, which is just for key in dictionary, gets you all the values of the keys so we may use it print the key and its associated value. If you want, you may even be more explicit and write the loop so that it's clear that you're looping over the keys. You can also loop over the values and you can loop over the keys and values together by writing a loop for a pair and using the method items of the dictionary.
You will notice that the keys and the values do not necessarily come out in alphabetical index. That index is not guaranteed for Python dictionaries and therefore, you shouldn't count on it. Just like lists, Python dicts are very useful and they're found everywhere in Python codes. They let you associate names with data and they're most natural to use where data items can be given unique labels. They're perhaps most appropriate to collect data items of different kinds. The classic example would be a dictionary that describes employee data, which may have an employee's name, address, and age.
We will now move on to Python Comprehensions, which in many cases are the best way to create lists and dictionaries.
- Writing and running Python in iPython
- Using Python lists and dictionaries
- Creating NumPy arrays
- Indexing and slicing in NumPy
- Downloading and parsing data files into NumPy and Pandas
- Using multilevel series in Pandas
- Aggregating data in Pandas