7.4. Built-in Methods

Table 7.1 lists the methods for dictionary objects. The clear(), copy(), get(), and update() methods were added recently in Python 1.5. setdefault() was introduced in 2.0.

Table 7.1. Dictionary Type Methods
dictionary methodOperation
dict.clear[a]()removes all elements of dictionary dict
dict.copy[a]()returns a (shallow [b]) copy of dictionary dict
dict.get(key, default=None)[a]for key key, returns value or default if key not in dictionary (note that default's default is None)
dict.has_key(key)returns 1 if key in dictionary dict, 0 otherwise
dict.items()returns a list of dict's (key, value) tuple pairs
dict.keys()returns list of dictionary dict's keys
dict.setdefault key, default=None)[c]similar to get(), but will set dict[key ...

Get Core Python Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.