Dictionary methods

Dictionaries, as hashable arrays rather than sequences, have a number of methods that are unique to them, and only a few of the sequence-specific methods are similar. The following is a list of the dictionary-specific methods:

  • len(dict): Returns the number of items in a dictionary.
  • dict[key]: Returns the value associated with key. If the specified key is not in the dictionary, an error is generated.
  • dict[key] = value: Sets the association of key to value.
  • del dict[key]: Deletes the indicated key, and its associated value, from the dictionary. If key doesn't exist, an error is generated.
  • key in dict: If the dictionary has the specified key name, returns True; otherwise, returns False.
  • key not in dict: If the dictionary ...

Get Learn Programming in Python with Cody Jackson 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.