In Table B.9, we list the full description and usage syntax for the dictionary built-in methods listed below.
Method Name
Operation
dict.clear[a]()
Removes all elements of dict
dict.copy[a]()
Returns a (shallow[b]) copy of dict
dict.fromkeys[c](seq, val=None)
Creates and returns a new dictionary with the elements of seq as the keys and val as the initial value (defaults to None if not given) for all keys
dict.get(key, default=None)[a]
For key key, returns value or default if key not in dict (note that default's default is None)
dict.has_key(key)
Returns true if key is in dict, False otherwise; partially deprecated by the in and not in operators in 2.2 but still provides a functional interface
dict.items()
Returns a list of the (key, value) tuple pairs of dict
dict.keys()
Returns a list of the keys of dict
dict.iter*[d]()
iteritems(), iterkeys(), itervalues() are all methods that behave the same as their non-iterator counterparts but return an iterator instead of a list
dict.pop[c](key[, default])
Similar to get() but removes and returns dict[key] if key present and raises KeyError if key not in dict and default not given
dict.setdefault(key, default=None)[e]
Similar to get(), but sets dict[key]=default if key is not already in dict
dict.update(dict2)[a]
Adds the key-value pairs of dict2 to dict
dict.values()
Returns a list of the values of dict
[a] New in Python 1.5
[b] More information regarding shallow and deep copies can be found in Section 6.19.
[c] New in Python 2.3.
[d] New in Python 2.2.
[e] New in Python 2.0.