dictionary in python


  • Dictionaries are a special data type not present in all programming languages. The main characteristic of a dictionary is that it stores arbitrary indexed unordered data types.
  • A dictionary is like a list, but more general. In a list, the index positions have to be integers; in a dictionary, the indices can be (almost) any type.
  • You can think of a dictionary as a mapping between a set of indices (which are called keys) and a set of values. Each key maps to a value.
  • The association of a key and a value is called a key-value pair or sometimes an item
  • The name dictionary should give you a clue about the purpose of this structure. An ordinary book is made for reading from start to finish.
  • A dictionary is more appropriate than a list in many situations. Here are some examples of uses of Python dictionaries:
    • Representing the state of a game board, with each key being a tuple of coordinates
    • Storing file modification times, with file names as keys
    • A digital telephone/address book

Creating and Using Dictionaries

Dictionaries are written like this:

    phonebook = {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'}
  • Dictionaries consist of pairs (called items) of keys and their corresponding values. In this example, the names are the keys and the telephone numbers are the values. Each key is separated from its value by a colon (:), the items are separated by commas, and the whole thing is enclosed in curly braces. An empty dictionary (without any items) is written with just two curly braces, like this: {}.

The dict Function

You can use the dict function1 to construct dictionaries from other mappings or from sequences of (key, value) pairs:

    >>> items = [('name', 'Gumby'), ('age', 42)]
    >>> d = dict(items)
    >>> d
    {'age': 42, 'name': 'Gumby'}
    >>> d['name']
    'Gumby'

Dictionary as a set of counters

Suppose you are given a string and you want to count how many times each letter appears. There are several ways you could do it:

  • You could create 26 variables, one for each letter of the alphabet. Then you could traverse the string and, for each character, increment the corresponding counter, probably using a chained conditional.
  • You could create a list with 26 elements. Then you could convert each character to a number (using the built-in function ord), use the number as an index into the list, and increment the appropriate counter.
  • You could create a dictionary with characters as keys and counters as the corresponding values. The first time you see a character, you would add an item to the dictionary. After that you would increment the value of an existing item.

Basic Dictionary Operations

The basic behavior of a dictionary in many ways mirrors that of a sequence:

  • len(d) returns the number of items (key-value pairs) in d.
  • d[k] returns the value associated with the key k.
  • d[k] = v associates the value v with the key k.
  • del d[k] deletes the item with key k
  • k in d checks whether there is an item in d that has the key k.