pyguides

Reference

Dictionary Methods

Methods available on Python dict objects.

  1. dict.clear()

    Removes all items from a dictionary, making it empty

  2. dict.copy()

    Returns a shallow copy of a dictionary, creating a new dictionary with the same key-value pairs

  3. dict.fromkeys()

    Creates a new dictionary with keys from an iterable and values set to a specified value

  4. dict.get()

    Returns the value for a key, or a default value if the key is not found

  5. dict.items()

    Returns a view object that displays a list of dictionary's key-value tuple pairs

  6. dict.keys()

    Returns a view object that displays a list of all keys in a dictionary

  7. dict.pop()

    Removes and returns the value for a specified key, optionally with a default value if the key does not exist

  8. dict.popitem()

    Removes and returns the last key-value pair from a dictionary as a tuple

  9. dict.setdefault()

    Returns the value for a key, inserting a default if the key does not exist

  10. dict.update()

    Updates a dictionary with key-value pairs from another dictionary or iterable

  11. dict.values()

    Returns a view object that displays a list of all values in a dictionary