Dictionaries in Python

Nina Vergara
3 min readAug 24, 2021

Defaultdict is a container datatype from the collections package in python. Utilizing this container enhances the readability of code with very few drawbacks.

Nested Dictionaries

Let’s say we would like to initialize a simple nested dictionary with these values:

outer_keys = ['ham','foo','bark']
inner_keys = ['turtle','bone','egg']
values = [1,2,3]

The pure python approach:

nested_dict = {}
for outer_k, inner_k, value in zip(outer_keys,inner_keys, values):
nested_dict[outer_k] = {}
nested_dit[outer_k][inner_k] = value

In the example above, regular dictionaries require us to specify the data type for each key. Only after we initialize the outer key with an empty dictionary are we allowed to populate the nested dictionary with the inner key and inner value.

The defaultdict approach:

from collections import defaultdictnested_dict = defaultdict(dict)
for outer_k, inner_k, value in zip(outer_keys,inner_keys, values):
nested_dit[outer_k][inner_k] = value

Here, we no longer have to initialize each outer key with an empty dictionary. We tell defaultdict to do this for us automatically when we plug in the dict datatype into the function.

An alternative approach:

nested_dict = dict(outer_keys, [{}]*len(outer_keys))
for outer_k, inner_k, value in zip(outer_keys,inner_keys, values):
nested_dict[outer_k][inner_k] = value

In the above example, a list of empty dictionaries is created by [{}]*len(outer_keys). This list was paired with the outer keys and passed into the built-in dict() function, which creates a dictionary from key, value iterables. The result is an initialized dictionary, with each outer key having an empty dictionary as its value.

Notice that we still have to loop through all of the outer keys, inner keys, and inner values. This, coupled with the complicated initialization process decreases readability.

Dictionary of Lists

Let’s say we want to create a dictionary of lists and append a sequence of integers to them. The data looks like this:

keys = ['hashbrown', 'card', 'cat']
values = [1,3,2,2]

The pure python approach:

dict_of_lists = {}
for key in keys:
dict_of_lists[key] = []
for val in values:
dict_of_lists.append(val)

Defaultdict approach:

dict_of_lists = defaultdict(list)
for key in keys:
for val in values:
dict_of_lists.append(val)

Dictionary of Integers

For the final example, we would like to count the number of times an item appears in a list. The item list is:

items = ['cool', 'can', 'bug', 'bug', 'can']

The pure python approach:

counts = {}
for item in items:
item_count = counts.get(item,0)
counts[item] = item_count + 1

This time, in order to check if an item is already placed in the dictionary, the .get(key, default_value) method is used. This method takes in a key, and a default value to return if the key is not present in the dictionary. If the key is present, then the method returns the key value.

Using the item as a key, the count associated count is grabbed, and if the item isn’t in the dictionary, then the item count is initialized to zero. Then, one is added to the item count and the updated count is assigned to the item key.

The defaultdict approach:

counts = defaultdict(int)
for item in items:
counts[item] += 1

This time around, when the default dict is initialized, we tell the function to return an integer as the default key-value. the += notation adds 1 to the item count and assigns it to the dictionary key in one swoop.

--

--