Python

python defaultdict Overview with Example : Implementation

Python defaultdict is a map-like data structure. The only difference between python defaultdict and Dictionary is a KeyError Exception. Python defaultdict never raises a KeyError exception. In the place of KeyError Exception It has default_factory component which has a default message(str) for missing key (KeyError Exception).

Python defaultdict Step By Step Implementation-

Step 1 :

Firstly, In this step, We will import the defaultdict class from collections module.

from collections import defaultdict 

Step 2:

Secondly, The syntax for instantiating defaultdict object is-

defaultdict(default_factory)

Here default_factory is a function that returns the default message for missing key.

defaultdict Example-

For example, let’s create a dummy scenario.

def default_message(): 
    return "Key is not there"
      
defaultdict_obj = defaultdict(default_message) 
defaultdict_obj ["key1"] = "value1"
defaultdict_obj ["key2"] = "value2"

Step 3:

Let’s look for an available key in defaultdict. Try the below code.

print(defaultdict_obj["key3"])

When we run the above code we get the output- “Key is not there”.

Let’s run the complete code together and see the output.

 

Few Variations in Defaultdict:

 

1. Python Defaultdict lambda-

Rather than creating a exaternal function for defaultdict(default_factory) We can also use the lambda function.

from collections import defaultdict
 
defaultdict_obj = defaultdict(lambda: "Key is missing") 
defaultdict_obj ["key1"] = "value1" 
defaultdict_obj ["key2"] = "value2"
print(defaultdict_obj["key3"])

the output of the code is –

Defaultdict lambda Example

 

2, Defaultdict with int as default factory:

When we provide int class as the default factory function. Its default value will be zero.

from collections import defaultdict
defaultdict_obj = defaultdict(int) 
defaultdict_obj ["key1"] = "value1" 
defaultdict_obj ["key2"] = "value2"
print(defaultdict_obj["key3"])

The output is-

Defaultdict lambda with int as default factory

3. Defaultdict with list as default factory:

In the similar way, When we use the list as the default factory. It returns the empty list when a key element is not available.

from collections import defaultdict
defaultdict_obj = defaultdict(list) 
defaultdict_obj ["key1"] = "value1" 
defaultdict_obj ["key2"] = "value2"
print(defaultdict_obj["key3"])

Here is the output for list default factory example.

 

Defaultdict lambda with list as default factory

 

3. Defaultdict with Set as default factory –

in addition to list and int, We can use also Set object as the default factory object. Here I am adding a code sample to the demo set in defaultdict.

from collections import defaultdict
defaultdict_obj = defaultdict(set) 
defaultdict_obj ["key1"].add(1)
defaultdict_obj ["key1"].add(11)
defaultdict_obj ["key2"].add(2)
print(defaultdict_obj["key3"])
print(defaultdict_obj)

Let’s run this code and see-

 

defaultdict with set

 

Well, I hope you must understand the basics of defaultdict. Please give your views on it.

Thanks 

Data Science Learner Team