Dict methods
The pre-defined functions (methods) allow manipulation, querying, and management of dictionary objects. Below is a detailed explanation of each method, including the nine listed in the announcement and the two additional standard methods (setdefault() and fromkeys()).
Creates a new dictionary with the specified keys, all assigned the same value (default is None).
Syntax: new_dict = dict.fromkeys(keys, value=None)
Key Notes:
- The static method is called on the dict class, not an instance.
- Useful for initializing dictionaries with default values.
Returns the value for the specified key. If the key does not exist, it returns None or a specified default value.
Syntax: value = dictobj.get(key, default=None).
Key Notes:
- Safer than direct key access (d[key]) as it avoids KeyError.
- Commonly used for accessing values with fallback options.
Returns a view object of the dictionary’s keys.
Syntax: keys = dictobj.keys()
Key Notes:
- The view object is dynamic, reflecting changes to the dictionary.
- Useful for iterating over keys.
Returns a view object of the dictionary’s values.
Syntax: values = dictobj.values().
Key Notes:
- Like keys(), the view object is dynamic.
- Useful for accessing all values without keys.
Returns a view object of the dictionary’s key-value pairs as tuples.
Syntax: items = dictobj.items().
Key Notes:
- Useful for iterating over both keys and values simultaneously.
- The view object is dynamic, reflecting dictionary changes.
Updates the dictionary with key-value pairs from another dictionary or iterable, overwriting existing keys.
Syntax: dictobj.update(other).
Key Notes:
- Modifies the dictionary in place.
- Can accept another dictionary or an iterable of key-value pairs (e.g., list of tuples).
Returns the value for the specified key. If the key does not exist, it inserts the key with the default value (or None) and returns it.
Syntax: value = dictobj.setdefault(key, default=None)
Key Notes:
- Combines get() and key insertion in one operation.
- Useful for initializing missing keys (e.g., in counting scenarios).
Removes all key-value pairs from the dictionary, leaving it empty.
Syntax: dictobj.clear().
Key Notes:
- Modifies the dictionary in place (same memory address).
- The dictionary becomes {} but retains its identity.
Creates a shallow copy of the dictionary, allowing modifications to the copy without affecting the original.
Syntax: dictobj2 = dictobj1.copy().
Key Notes:
- Returns a new dictionary with a different memory address.
- Shallow copy means nested objects (e.g., lists as values) are not deeply copied.
Removes and returns the value associated with the specified key. Raises KeyError if the key does not exist.
Syntax: value = dictobj.pop(key).
Key Notes:
- Modifies the dictionary by removing the specified key-value pair.
- Useful for extracting and deleting a specific entry.
Removes and returns the last inserted key-value pair as a tuple. Raises KeyError if the dictionary is empty.
Syntax: key_value = dictobj.popitem().
Key Notes:
- Since Python 3.7+, dictionaries maintain insertion order, so popitem() removes the last added pair.
- Useful for stack-like operations.
The del statement is a Python keyword used to remove objects or specific components of objects, such as dictionary key-value pairs, list elements, or entire variables. In the context of dictionaries, del is commonly used to delete a specific key-value pair or the entire dictionary.
- Removes the key-value pair associated with key.
- Raises KeyError if key does not exist.
- Removes the dictionary object entirely, making it inaccessible.
- Subsequent access raises NameError.