Piton

Python defaultdict

Python defaultdict

Python offers many built-in data structures, such as lists, tuples, and dictionaries, to save and manage data efficiently. Dictionaries provide an easy way to save data as key-value pairs. A key acts as an index and is used to retrieve data. Keys should be unique and immutable throughout the dictionary. Keys are mostly strings and integers, though the value of a key could be of any type, such as an integer, string, floating-point number, or complex number. Meanwhile, a dictionary can contain a collection, such as a list, tuple, or some other type of dictionary. A dictionary in Python is created using a pair of curly brackets, in which each key-value pair is separated by a comma.

What if you try to access or modify a specific key in a dictionary that does not exist? Well, in this case, the Python interpreter will raise the “KeyError” error and terminate the execution of the program.

Let us look at an example of the “KeyError” error.

#creating a dictionary
my_dict= 1:"Hello",2:"Welcome",3:"LinuxHint"
#accessing a key that does not exist
my_dict[4]

Output

To handle this error, Python provides the defaultdict type, a dictionary class that can be found in the collections module. This article covers the Python defaultdict in detail.

Explanation and Examples of defaultdict

The defaultdict is the alternative of the Python dictionary. It returns dictionary type objects. The defaultdict is a subclass of dict. In the case of a missing key, rather than raising a “KeyError” error, the default value is provided for the non-existent key. The syntax of defaultdict is as follows:

defaultdict(default_factory)

The defaultdict function takes the default_factory parameter as an argument. The default_factory function returns the default value for the defined defaultdict. If we try to access or modify a key that is missing, then the default_factory function returns the default value as result. If the default_factory function is missing, then the defaultdic will raise a “KeyError” error if there are any missing keys.

First, we will implement a defaultdic in our Python script to access both existing and missing keys.

#import the defaultdict from the collection module
from collections import defaultdict
#defining a function to return the default value
def my_func():
print("The key is not defined")
#creating a defualtdict
#passing the my_func() as an argument to defaultdict
my_dict =defaultdict(my_func)
#defining the keys and values
my_dict[1]="Hello"
my_dict[2]="Welcome"
my_dict[3]="LinuxHint"
#accessing the values using keys
print(my_dict[1])
print(my_dict[2])
print(my_dict[3])
#accessing the missing or not defined key
my_dict[4]

Output

The [4] is the missing key. So, instead of raising a “KeyError” error, it calls the default_factory function, which returns the default value.


A lambda function, known as an anonymous function, can also be used as the default_factory parameter.

Next, we will implement a lambda function and pass it as an argument to the defaultdict.

#import the defaultdict from the collection module
from collections import defaultdict
#creating a defualtdict
#passing the Lamnda function as an argument to defaultdict
my_dict =defaultdict(lambda:"The key is not defined")
#defining the keys and values
my_dict[1]="Hello"
my_dict[2]="Welcome"
my_dict[3]="LinuxHint"
#accessing the values using keys
print(my_dict[1])
print(my_dict[2])
print(my_dict[3])
#accessing the missing or not defined key
print(my_dict[4])

The defaultdict calls the default_factory to provide the default value, but the __missing()__ function provides the default value. The __missing()__ function is a built-in function in Python that takes the default_factory parameter as an argument. If the default_factory parameter is not defined, the __missing()__ function raises the “KeyError” error.

Here, we will use the __missing()__ function in our Python script.

#import the defaultdict from the collection module
from collections import defaultdict
#defining a function to return the default value
def my_func():
print("The key is not defined")
#creating a defualtdict
#passing the my_func() as an argument to defaultdict
my_dict =defaultdict(my_func)
#defining the keys and values
my_dict[1]="LinuxHint"
#accessing the values using keys
print(my_dict[1])
#accessing the missing or not defined key
my_dict.__missing__(2)

Output

The list and int classes can also be used as default_factory parameters with defaultdict.

List as default_factory Parameter

The defaultdict is created with the list values when the list class is passed a default_factory parameter.

#import the defaultdict from the collection module
from collections import defaultdict
# defining a defaultdict
# passing list class as an default_factory
my_dict = defaultdict(list)
for i in range(7):
my_dict[i].append(i)
#printing the values
print(my_dict)

Output

The output shows the values in the list. As you can see, a defaultdict that contains the key and values has been created.

Integers as default_factory Parameter

If we pass the int class as a default_factory parameter, then we can create a defaultdict that only contains integers. We will create a list of integers and then save it to the defaultdict.

#import the defaultdict from the collection module
from collections import defaultdict
# defining a defaultdict
# passing int class as an default_factory
my_dict = defaultdict(int)
#creating a list of integers
my_list=[1,2,3]
for i in my_list:
my_dict[i]+=1
#printing the values
print(my_dict)

Output

In the given output, the integer values before the colon (:) are keys, while the integer values after the colon are values.

Conclusion

The dafualtdict is a dictionary-like class present in the collections module. It works like a Python dictionary but does not raise a “KeyError” when a key is not found. This article explains the defaultdict in detail with various examples.

Middle mouse button not working in Windows 10
The middle mouse button helps you scroll through long webpages and screens with a lot of data. If that stops, well you will end up using the keyboard ...
How to change Left & Right mouse buttons on Windows 10 PC
It's quite a norm that all computer mouse devices are ergonomically designed for right-handed users. But there are mouse devices available which are s...
Emulate Mouse clicks by hovering using Clickless Mouse in Windows 10
Using a mouse or keyboard in the wrong posture of excessive usage can result in a lot of health issues, including strain, carpal tunnel syndrome, and ...