Dictionary Methods

A dict object has a number of member methods. Many of these maintain the values in a dict. Others retrieve parts of the dict as a sequence, for use in a for statement. In the following definitions, d is a dict object.

The following transformation functions update a dict object.

d. clear

Remove all items from the dict.

d. copy → dictionary

Copy the dict to make a new dict. This is a shallow copy. All objects in the new dict are references to the same objects as the original dict.

d. setdefault ( key , [ default ]) → object

Similar to d. get( key ) and d [ key ] - get the item with the given key. However, this sets a default value to the supplied object.

d. update ( new , [ default ]) → object

Merge values from the new dict into the original dict, adding or replacing as needed. It is equivalent to the following Python statement. for k in new.keys(): d[k]= new[k]

d. pop ( key , [ value ]) → object

Remove the given key from the dict, returning the associated value. If the key does not exist, return the optional value provided in the pop call.

The following accessor methods provide information about a dict.

d. get ( key , [ default ]) → object

Get the item with the given key , similar to d [ key ]. If the key is not present, supply default instead.

d. has_key ( key ) → boolean

If there is an entry in the dict with the given key , return True, otherwise return False.

d. items → sequence

Return all of the items in the dict as a sequence of (key,value) tuples. Note that these are returned in no particular order.

d. keys → sequence

Return all of the keys in the dict as a sequence of keys. Note that these are returned in no particular order.

d. values → sequence

Return all the values from the dict as a sequence. Note that these are returned in no particular order.