Guide to Python Dictionaries

What is a Python Dictionary?

Second to a Python list, the dictionary or “dict” is a place in memory to store a series of values – also called a collection. The dictionary is special because values are not referenced in order using a numerical index. Rather, in a dictionary, values are referenced with a user-defined key, just as words in a physical dictionary are “keys” associated with the “value” of their meaning. This key is usually a string, but could be any number of data types.

my_dict = {'my_key' : 'my_value'}

For example, instead of referring to the first value in a list with my_list[0], one refers to any dictionary element by its key:

>>> my_dict['my_key']

These explicit references are more legible than list index notation and improve the maintainability and performance of code in most situations.

Additionally, key-value combinations allow complex hierarchies of nested data. As words in a dictionary are keys to the values of their definitions, so letters of the alphabet are keys to the values of words themselves. Such complexity in data in structure is often necessary, when dealing with complex data. With this special feature, a dictionary lives somewhere between lists and user-defined classes. Python dictionaries are more feature-rich than lists, but don’t require as much effort as a user-defined class with unique attributes and methods.

How to Create and Reference Python Dictionaries

There are several ways to declare a dictionary, depending on the situation. The simplest is to enclose the keys and values in curly braces, like so:

my_dict = {'key1': 1, 'key2': 2}

You can also pass key-value pairs to the dict keyword constructor, though this is less common:

my_dict = dict(key1 = 1, key2 = 2)

Assigning values on declaration is useful when returning a dictionary with dynamic values, or as part of a lambda or comprehension. Both the keys and the values may be references to variables defined elsewhere, allowing dynamic assignment.

Sometimes it’s necessary to declare an empty dictionary, as values might be added later, but other parts of the code need something to reference in the meantime.

To declare an empty dictionary:

my_dict = {}
my_dict = dict()

Values may then be appended to this dictionary when they become available with the assignment operator:

my_dict['key'] = 123

>>> my_dict
{'key': 123}

Python dictionaries are stored and referenced like any other variable. In fact, dictionaries can be stored within dictionaries, and often are. In this case, just refer to the stored dictionary as you would any other value – by its key.

my_dict = {
            'a_key': 'a_value',
            'another_key': 'another_value',

It’s polite to use whitespace in a way that clearly indicates nested layers while maintaining consistency with Python best practices. The specific format may be determined by an IDE auto-formatter, or a pre-deployment linter.

Now, we can refer to the nested dictionary by its key:

my_variable = my_dict['my_nested_dict']

The Dictionary Comprehension – Less is More

A more advanced technique for defining a dictionary is using the Python dictionary comprehension. Like a list comprehension, a dictionary comprehension generates a dynamically-sized dictionary in a format more concise than the notation above:

automatic_dictionary = {key: value for (key, value) in < some_iterable >}

Any iterable object that could be associated in terms of keys and values, a list of tuples for example, easily becomes a dictionary with a single line of code. Depending on the size of the iterable, the dictionary comprehension notation can be a space-saver (and a lifesaver!) making code that much more “Pythonic.”

Practical Use Cases

Let’s say we need to quickly model and store some data without the boiler-plate of a class or hairy SQL statements. For example, we need to store some data about users of a website.

A User class might look like…

Back to Top