6.4 Dictionaries

Dictionaries, also known as associative arrays or content-addressable memories in other programming languages, store collections of objects, each of which has a unique name (or key). Objects are addressed by name, rather than by number:

pyxplot> myDict = {’red’:colors.red, ’green’:colors.green}
pyxplot> myDict[’blue’] = colors.blue
pyxplot> print myDict[’green’]
cmyk(1,0,1,0)
pyxplot> call myDict.delete(’green’)
pyxplot> call myDict.delete(’blue’)
pyxplot> myDict[’purple’] = colors.purple
pyxplot> print myDict
{"purple":cmyk(0.45,0.86,0,0), "red":cmyk(0,1,1,0)}

As the first line of this example shows, dictionaries can be created by enclosing a list of key–value pairs in curly brackets. As in python, a colon separates each key from its corresponding value, while the list of key–value pairs are comma-separated. That is, the general syntax is:

{ key1:value1 , key2:value2 , ... }

It is also possible to generate an empty dictionary, as {}. Items can later be referenced or assigned by name, where the name is placed in square brackets after the name of the dictionary. Items can be deleted with the dictionary’s delete(key) method.

It is not an error to assign an item to a name which is already defined in the dictionary; the new assignment overwrites the old object with that name. It is, however, an error to attempt to access a key which is not defined in the dictionary. The method hasKey(key) may be used to test whether a key is defined before attempting to access it.

Unlike in python, keys must be strings.