Python – Dictionaries

What are they?

A dictionary is an unordered set of key:value pairs where the value is mutable, and the
key is unique and hashable.  A dictionary is defined within a set of braces {}.
Objects such as dictionaries, lists, or other mutable structures may not be used as keys.
Objects such as integer, string, or other non-mutable types may be used as an index.

How are they created?

Dictionaries can be created using various techniques.  A variable name with an assignment operator
and an empty pair of braces will create and empty dictionary.
d = {}

You can also use the dict() function:

 d = dict()
d = dict(one=1, two=2, three=3) or …
d = dict(‘name’:’Ted’, ‘age’:27, ‘height’:74)

Other methods are shown in the example code to follow.

What are they used for?

Dictionaries are very fast for finding data values based upon the key value (similar to a database).
As such they can be used for searching large amounts of data very quickly.  If we need to store, retrieve,
and modify data values, associated by a unique key, the dictionary data structure provides these features.


I’ve included several examples of creating, and using dictionaries with the Jupyter Notebook.

*NOTE:  Click on the image to enlarge.

The DictWriter will write rows to the csv file.  Fieldnames are used as the column names, these are
mandatory.  I’ve used the DictWriter class below to insert an unusual car into the csv file.


In this blog, I’ve explained the basics of Dictionary data structures.  I’ve also given examples of their
usage, creation, and methods.  To find further information, and examples, review the references listed below.

Knowledge is Power!


Python Dictionaries: