Defaultdict in Python
In this tutorial, we will study What is defaultdict in Python We will understand it with the aid of certain examples. Before this, we will have a look at dictionaries used in python.
Dictionary
A dictionary is one of the data structures or collections used in python. It is mainly a key-value pair that is used to store the data. A Key is written along with its value. Both are separated by a colon ( : ). It is an unordered collection in older versions of python but is an ordered collection in newer versions of python that as python 3.7.
Here, order refers to the arrangement of data in a dictionary.
It does not support the duplication of keys. Each key should be unique. It does not support multiple data of the same key name.
Syntax:
Dict_name = {
"name1": "A",
"name2": "B",
"name3": 87
}
Here, name1, name2, and name3 refer to key names, and A, B, and 87 refer to corresponding values associated with keys. Each key is separated from the other through a comma. One should keep in mind the case sensitivity of the dictionary otherwise it would create diversity in the desired output.
Now, let us see some illustrations to understand dictionaries in python better.

Explanation: This image shows the code written to create a dictionary named first_dict which contains 3 keys and their corresponding values. We are trying to print the value present in the first index.

Explanation: The image shows the output generated on running the above code. The value “Java” was present in the first index and that is being successfully printed here.

Explanation: The image shows the code to access the key at index 7. Although it is not present in our dictionary, we are trying to access it. Let’s see what output it throws.

Explanation: The image shows the output generated on running the above code. Here, the KeyError arises as no key is present at index 7. Keyerror in python could create a problem.
defaultdict
To solve the keyerror problem arising in a dictionary, defaultdict comes into action.
It is also one of the containers of the collection modules in python. To be precise, it is a subdivision of dictionary class.
We saw that when we try to access a key that is absent in a dictionary, it throws an error but defaultdict would provide a default value on the basis of the type of dictionary being created.
It is necessary to import defaultdict in order to use it.
It can be imported as: from collections import defaultdict
Now, we will provide int, set, and list as parameters to defaultdict and check the effect on output.
When int is given as the parameter.

Explanation: This image demonstrates the defaultdict when the parameter is int. Here, no values are provided to keys 7 and 8 so let’s see what output it throws.

Explanation: This image demonstrates the output for the code written above. Although no values were provided to keys 7 and 8. The output for both is 0. Here no key error is thrown as before. This is only due to the application of defaultdict.
When the set is given as the parameter.

Explanation: This image demonstrates the defaultdict when the parameter is set. Here, no value is provided to the key fourth. So, let’s see what output it throws in this case.

Explanation: This image demonstrates the output for the code written above. Although no value was provided to key fourth. The output generated here is an empty set for the key fourth and the inputted values are printed for the rest of the keys. No key error is thrown here due to the application of defaultdict.
When the list is given as the parameter.

Explanation: This image demonstrates the defaultdict when the parameter is the list. Here, no values are provided to keys third and fourth. So, let’s see what output it throws in this case.

Explanation: This image demonstrates the output for the code written above. Although no values were provided to keys third and fourth. The output generated here is an empty list for the keys third and fourth and the inputted values are printed for the rest of the keys. Here also, no key error is thrown due to the application of defaultdict.