You are here: Home > Dive Into Python > Native Datatypes | << >> | ||||
Dive Into PythonPython from novice to pro |
You'll get back to your first Python program in just a minute. But first, a short digression is in order, because you need to know about dictionaries, tuples, and lists (oh my!). If you're a Perl hacker, you can probably skim the bits about dictionaries and lists, but you should still pay attention to tuples.
One of Python's built-in datatypes is the dictionary, which defines one-to-one relationships between keys and values.
A dictionary in Python is like a hash in Perl. In Perl, variables that store hashes always start with a % character. In Python, variables can be named anything, and Python keeps track of the datatype internally. |
A dictionary in Python is like an instance of the Hashtable class in Java. |
A dictionary in Python is like an instance of the Scripting.Dictionary object in Visual Basic. |
Example 3.1. Defining a Dictionary
>>> d = {"server":"mpilgrim", "database":"master"} >>> d {'server': 'mpilgrim', 'database': 'master'} >>> d["server"] 'mpilgrim' >>> d["database"] 'master' >>> d["mpilgrim"] Traceback (innermost last): File "<interactive input>", line 1, in ? KeyError: mpilgrim
Example 3.2. Modifying a Dictionary
>>> d {'server': 'mpilgrim', 'database': 'master'} >>> d["database"] = "pubs" >>> d {'server': 'mpilgrim', 'database': 'pubs'} >>> d["uid"] = "sa" >>> d {'server': 'mpilgrim', 'uid': 'sa', 'database': 'pubs'}
Note that the new element (key 'uid', value 'sa') appears to be in the middle. In fact, it was just a coincidence that the elements appeared to be in order in the first example; it is just as much a coincidence that they appear to be out of order now.
Dictionaries have no concept of order among elements. It is incorrect to say that the elements are “out of order”; they are simply unordered. This is an important distinction that will annoy you when you want to access the elements of a dictionary in a specific, repeatable order (like alphabetical order by key). There are ways of doing this, but they're not built into the dictionary. |
When working with dictionaries, you need to be aware that dictionary keys are case-sensitive.
Example 3.3. Dictionary Keys Are Case-Sensitive
>>> d = {} >>> d["key"] = "value" >>> d["key"] = "other value" >>> d {'key': 'other value'} >>> d["Key"] = "third value" >>> d {'Key': 'third value', 'key': 'other value'}
Example 3.4. Mixing Datatypes in a Dictionary
>>> d {'server': 'mpilgrim', 'uid': 'sa', 'database': 'pubs'} >>> d["retrycount"] = 3 >>> d {'server': 'mpilgrim', 'uid': 'sa', 'database': 'master', 'retrycount': 3} >>> d[42] = "douglas" >>> d {'server': 'mpilgrim', 'uid': 'sa', 'database': 'master', 42: 'douglas', 'retrycount': 3}
Example 3.5. Deleting Items from a Dictionary
>>> d {'server': 'mpilgrim', 'uid': 'sa', 'database': 'master', 42: 'douglas', 'retrycount': 3} >>> del d[42] >>> d {'server': 'mpilgrim', 'uid': 'sa', 'database': 'master', 'retrycount': 3} >>> d.clear() >>> d {}
Further Reading on Dictionaries
- How to Think Like a Computer Scientist teaches about dictionaries and shows how to use dictionaries to model sparse matrices.
- Python Knowledge Base has a lot of example code using dictionaries.
- Python Cookbook discusses how to sort the values of a dictionary by key.
- Python Library Reference summarizes all the dictionary methods.
<< Testing Modules |
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | |
Introducing Lists >> |