next up previous contents
Next: Miscellaneous New Built-in Up: Recent Additions Previous: Regular Expressions

Generalized Dictionaries

The keys of dictionaries are no longer restricted to strings --- they can be any immutable basic type including strings, numbers, tuples, or (certain) class instances. (Lists and dictionaries are not acceptable as dictionary keys, in order to avoid problems when the object used as a key is modified.)

Dictionaries have two new methods: d.values() returns a list of the dictionary's values, and d.items() returns a list of the dictionary's (key, value) pairs. Like d.keys(), these operations are slow for large dictionaries. Examples:

        >>> d = {100: 'honderd', 1000: 'duizend', 10: 'tien'}
        >>> d.keys()
        [100, 10, 1000]
        >>> d.values()
        ['honderd', 'tien', 'duizend']
        >>> d.items()
        [(100, 'honderd'), (10, 'tien'), (1000, 'duizend')]
        >>>



guido@cwi.nl