Word references are an advantageous method to store information for later recovery by name (key). Keys must be one of a kind, changeless articles, and are ordinary strings. The qualities in a word reference can be anything. For some applications, the qualities are straightforward sorts, for example, whole numbers and strings. 

It gets all the more fascinating when the qualities in a word reference are accumulations (records, dicts, and so on.) For this situation, the worth (a vacant rundown or dict) must be introduced the first run through a given key is utilized. While this is generally simple to do physically, the defaultdict type mechanizes and improves these sorts of tasks. 

A defaultdict works precisely like a typical dict, however, it is instated with a capacity (“default processing plant”) that takes no contentions and gives the default an incentive to a nonexistent key. 

A defaultdict will never raise a KeyError. Any key that doesn’t exist gets the worth returned by the default manufacturing plant.

>>> from collections import defaultdict

>>> ice_cream = defaultdict(lambda: ‘Vanilla’)


>>> ice_cream = defaultdict(lambda: ‘Vanilla’)

>>> ice_cream[‘Sarah’] = ‘Chunky Monkey’

>>> ice_cream[‘Abdul’] = ‘Butter Pecan’

>>> print ice_cream[‘Sarah’]

Chunky Monkey

>>> print ice_cream[‘Joe’]



Make certain to pass the capacity article to defaultdict(). Try not to call the capacity, for example defaultdict(func), not defaultd.ict(func()). 

In the accompanying model, a defaultdict is utilized for tallying. The default manufacturing plant is int, which thusly has a default estimation of zero. (Note: “lambda: 0″ would likewise work in this circumstance). For every nourishment in the rundown, the worth is increased by one where the key is the nourishment. We don’t have to ensure the nourishment is as of now a key – it will utilize the default estimation of zero

>> from collections import defaultdict

>>> food_list = ‘spam spam spam spam spam spam eggs spam’.split()

>>> food_count = defaultdict(int) # default value of int is 0

>>> for food in food_list:

…     food_count[food] += 1 # increment element’s value by 1

defaultdict(<type ‘int’>, {‘eggs’: 1, ‘spam’: 7})


In the next example, we start with a list of states and cities. We want to build a dictionary where the keys are the state abbreviations and the values are lists of all cities for that state. To build this dictionary of lists, we use a defaultdict with a default factory of list. A new list is created for each new key.

>>> from collections import defaultdict

>>> city_list = [(‘TX’,’Austin’), (‘TX’,’Houston’), (‘NY’,’Albany’), (‘NY’, ‘Syracuse’), (‘NY’, ‘Buffalo’), (‘NY’, ‘Rochester’), (‘TX’, ‘Dallas’), (‘CA’,’Sacramento’), (‘CA’, ‘Palo Alto’), (‘GA’, ‘Atlanta’)]


>>> cities_by_state = defaultdict(list)

>>> for state, city in city_list:

…     cities_by_state[state].append(city)

for state, cities in cities_by_state.iteritems():

…     print state, ‘, ‘.join(cities)

NY Albany, Syracuse, Buffalo, Rochester

CA Sacramento, Palo Alto

GA Atlanta

TX Austin, Houston, Dallas