Title: How To Use Default Dicts
Slug: how_to_use_default_dicts
Summary: How To Use Default Dicts in Python.
Date: 2016-01-23 12:00
Category: Python
Tags: Basics
Authors: Chris Albon

Interesting in learning more? Check out Fluent Python

Preliminaries


In [1]:
import collections

Create A DefaultDict

Default Dicts work just like regular dictionaries, except a key is called that doesn't have a value, a default value (note: value, not key) is supplied.


In [2]:
# Create a defaultdict with the default value of 0 (int's default value is 0)
arrests = collections.defaultdict(int)

Add A New Key With A Value


In [3]:
# Add an entry of a person with 10 arrests
arrests['Sarah Miller'] = 10

In [4]:
# View dictionary
arrests


Out[4]:
defaultdict(int, {'Sarah Miller': 10})

Add A New Key Without A Value


In [5]:
# Add an entry of a person with no value for arrests,
# thus the default value is used
arrests['Bill James']


Out[5]:
0

In [6]:
# View dictionary
arrests


Out[6]:
defaultdict(int, {'Bill James': 0, 'Sarah Miller': 10})