A dictionary is like an address-book where you can find the address or contact details of a person by knowing only his/her name i.e. we associate keys (name) with values (details). Note that the key must be unique - you cannot find out the correct information if you have two persons with the exact same name.
A word of caution: you can use only immutable values (like strings) for keys of a dictionary but you can use either immutable or mutable values for values. This basically means to say that you can use only simple objects as keys.
Pairs of keys and values are specified in a dictionary by using the notation d = {key1 : value1, key2 : value2 }. Notice that the key and value pairs are separated by a colon, and the pairs are separated themselves by commas and all this is enclosed in a pair of curly brackets.
Remember that key/value pairs in a dictionary are not ordered in any manner. If you want a particular order, then you will have to sort them yourself.
The dictionaries that you will be using are objects of class dict.
Example 9.4. Using dictionaries
#!/usr/bin/python # Filename : dict.py # 'ab' is short for 'a'ddress'b'ook ab = { 'Swaroop' : 'python@g2swaroop.net', 'Miguel' : 'miguel@novell.com', 'Larry' : 'larry@wall.org', 'Spammer' : 'spammer@hotmail.com' } print "Swaroop's address is %s" % ab['Swaroop'] # Adding a key/value pair ab['Guido'] = 'guido@python.org' # Deleting a key/value pair del ab['Spammer'] print "\nThere are %d contacts in the address-book\n" % len(ab) for name, address in ab.items(): print 'Contact %s at %s' % (name, address) if ab.has_key('Guido'): print "\nGuido's address is %s" % ab['Guido']
$ python dict.py Swaroop's address is python@g2swaroop.net There are 4 contacts in the address-book Contact Swaroop at python@g2swaroop.net Contact Larry at larry@wall.org Contact Miguel at miguel@novell.com Contact Guido at guido@python.org Guido's address is guido@python.org
We create the dictionary ab using the notation we have already discussed. We then access key/value pairs by specifying the key using the indexing operator as discussed in the context of lists and tuples.
We can also add new key/value pairs using the indexing operator - we just access that key and assign that value, as we have done for Guido in the above case.
We can delete key/value pairs using our old friend - the del statement. We specify which key/value pair by specifying the dictionary followed by an index operation for that key with the del statement (there is no need to use the value for this operation).
Next, we access each key/value pair of the dictionary using the items method of the dictionary which returns a list of tuples where each tuple contains two items - the key followed by the value. We retrieve these two items in each tuple and assign them to the variables name and address. Then, we use the for..in loop to iterate over this and we print these values in the for-block.
We can check if a key/value pair exists using the has_key method of a dictionary as used above. You can see documentation for the complete list of methods of the dict class using help(dict).
Keyword Arguments and Dictionaries. On a different note, if you have used keyword arguments in your functions, you have already used dictionaries! Just think about it - the key/value pair is specified by you in the parameter list of the function definition and when you access variables within your function, it is just a key access of a dictionary (which is called the symbol table in compiler design terminology).