One of Python’s built-in datatypes is the dictionary, which defines one-to-one relationships between keys and values.
Note: Python vs. Perl: Dictionaries 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.
Note: Python vs. Java: Dictionaries A dictionary in Python is like an instance of the Hashtable class in Java.
Note: Python vs. Visual Basic: Dictionaries A dictionary in Python is like an instance of the Scripting.Dictionary object in Visual Basic.
3.1.1. Defining Dictionaries¶
Example 3.1. Defining a Dictionary¶
>>> d = {"server":"mpilgrim", "database":"master"} (1)
>>> d
{'server': 'mpilgrim', 'database': 'master'}
>>> d["server"] (2)
'mpilgrim'
>>> d["database"] (3)
'master'
>>> d["mpilgrim"] (4)
Traceback (innermost last):
File "<interactive input>", line 1, in ?
KeyError: mpilgrim
- First, you create a new dictionary with two elements and assign it to the variable d. Each element is a key-value pair, and the whole set of elements is enclosed in curly braces.
- ‘server’ is a key, and its associated value, referenced by d[“server”], is ‘mpilgrim’.
- ‘database’ is a key, and its associated value, referenced by d[“database”], is ‘master’.
- You can get values by key, but you can’t get keys by value. So d[“server”] is ‘mpilgrim’, but d[“mpilgrim”] raises an exception, because ‘mpilgrim’ is not a key.
3.1.2. Modifying Dictionaries¶
Example 3.2. Modifying a Dictionary¶
>>> d
{'server': 'mpilgrim', 'database': 'master'}
>>> d["database"] = "pubs" (1)
>>> d
{'server': 'mpilgrim', 'database': 'pubs'}
>>> d["uid"] = "sa" (2)
>>> d
{'server': 'mpilgrim', 'uid': 'sa', 'database': 'pubs'}
- You can not have duplicate keys in a dictionary. Assigning a value to an existing key will wipe out the old value.
- You can add new key-value pairs at any time. This syntax is identical to modifying existing values. (Yes, this will annoy you someday when you think you are adding new values but are actually just modifying the same value over and over because your key isn’t changing the way you think it is.)
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.
Note: Dictionaries are unordered 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" (1)
>>> d
{'key': 'other value'}
>>> d["Key"] = "third value" (2)
>>> d
{'Key': 'third value', 'key': 'other value'}
- Assigning a value to an existing dictionary key simply replaces the old value with a new one.
- This is not assigning a value to an existing dictionary key, because strings in Python are case-sensitive, so ‘key’ is not the same as ‘Key’. This creates a new key/value pair in the dictionary; it may look similar to you, but as far as Python is concerned, it’s completely different.
Example 3.4. Mixing Datatypes in a Dictionary¶
>>> d
{'server': 'mpilgrim', 'uid': 'sa', 'database': 'pubs'}
>>> d["retrycount"] = 3 (1)
>>> d
{'server': 'mpilgrim', 'uid': 'sa', 'database': 'master', 'retrycount': 3}
>>> d[42] = "douglas" (2)
>>> d
{'server': 'mpilgrim', 'uid': 'sa', 'database': 'master',
42: 'douglas', 'retrycount': 3}
- Dictionaries aren’t just for strings. Dictionary values can be any datatype, including strings, integers, objects, or even other dictionaries. And within a single dictionary, the values don’t all need to be the same type; you can mix and match as needed.
- Dictionary keys are more restricted, but they can be strings, integers, and a few other types. You can also mix and match key datatypes within a dictionary.
3.1.3. Deleting Items From Dictionaries¶
Example 3.5. Deleting Items from a Dictionary¶
>>> d
{'server': 'mpilgrim', 'uid': 'sa', 'database': 'master',
42: 'douglas', 'retrycount': 3}
>>> del d[42] (1)
>>> d
{'server': 'mpilgrim', 'uid': 'sa', 'database': 'master', 'retrycount': 3}
>>> d.clear() (2)
>>> d
{}
- del lets you delete individual items from a dictionary by key.
- clear deletes all items from a dictionary. Note that the set of empty curly braces signifies a dictionary without any items.
Further Reading on Dictionaries
- How to Think Like a Computer Scientist (http://www.ibiblio.org/obp/ thinkCSpy/) teaches about dictionaries and shows how to use dictionaries to model sparse matrices (http://www.ibiblio.org/obp/thinkCSpy/chap10.htm).
- Python Knowledge Base (http://www.faqts.com/knowledge-base/index.phtml/ fid/199/) has a lot of example code using dictionaries (http:// www.faqts.com/knowledge-base/index.phtml/fid/541).
- Python Cookbook (http://www.activestate.com/ASPN/Python/Cookbook/) discusses how to sort the values of a dictionary by key (http:// www.activestate.com/ASPN/Python/Cookbook/Recipe/52306).
- Python Library Reference (http://www.python.org/doc/current/lib/) summarizes all the dictionary methods (http://www.python.org/doc/current/ lib/typesmapping.html).