Subscribe Contact

Home  »  Reference Materials



Dictionary Methods

The Python dictionary data structure has a set of built-in methods that we can use to work with dictionaries. For more about dictionaries see the Dictionaries page of this eBook. The following table outlines those methods, current as of Python version 3.9. Additional information about dictionary methods is available at Python.org here.

Dictionary Method Description
clear() Removes all the elements from the specified dictionary. General Form:
dictionary_var.clear()
copy() Returns a copy of the specified dictionary. General Form:
dictionary_var.copy()
fromkeys() Returns a dictionary with the specified keys and data values. General Form:
dictionary_var.fromkeys(keys[, values])
get() Returns the value associated with the specified key. General Form:
dictionary_var.get(keyname[, value])
items() Returns a list containing a tuple for each key value pair in the specified dictionary. General Form:
dictionary_var.items()
keys() Returns a list of the specified dictionary's keys. General Form:
dictionary_var.keys()
pop() Removes the value based on the specified key. General Form:
dictionary_var.pop(keyname[, value])
popitem() Removes the last inserted key-value pair from the specified dictionary. General Form:
dictionary_var.popitem()
setdefault() Returns the value associated with the specified key. If the specified key does not exist, it will be inserted with the specified (optional) value. General Form:
dictionary_var.setdefault(key[, value])
update() Updates the specified dictionary with the specified key-value pairs. General Form:
dictionary_var.update(iterable)
values() Returns a list of all values in the specified dictionary. General Form:
dictionary_var.values()


«  Previous : Reference : Date & Time Format Codes
Next : Reference : Escape Sequences  »




© 2023 John Gordon
Cascade Street Publishing, LLC