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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.