Home Python Python defaultdict vs. Dictionaries with examples

Python defaultdict vs. Dictionaries with examples

We’ll demonstrate with examples what defaultdict() is and how it differs from dict().

What are Python Dictionaries, and How do we use them?

We recommend you familiarize yourself with Python Dictionaries before moving on to defaultdict in Python. A dictionary in Python is a container for key-value pairs. Keys must be one-of-a-kind, unchangeable items. While a Python tuple can be used as a key, a Python list cannot because it is mutable.

{[11,32]:11,32:32}

As you can see, a list is not hashable. Let’s look at an example of defining a dictionary in Python.

new_dict={'x':31,'y':42,'z':53}
print(new_dict['z'])

The dict() function is also used.

additional_dict=dict((['x',31],['y',42],['z',53]))
print(additional_dict)

The keys() method is used to acquire a list of keys.

print(additional_dict.keys())

However, there is one issue. When we try to access a key that doesn’t exist, we get a KeyError.

print(additional_dict['d'])

How is defaultdict the solution?

In Python, we have a defaultdict to cope with this scenario. To begin, we’ll need to import it from the Python collections module.

from collections import defaultdict

Next, we create a Python function that returns a default value for any keys that aren’t initialized.

def defaultvalue():
  return 0

Using the defaultdict() function, we can now create a Python defaultdict. As a function argument, we pass the defaultvalue function. Remember that you must pass the function object; you do not need to use parentheses to call it.

additional_dict=defaultdict(defaultvalue)

Let’s define values for the keys ‘x’, ‘y’, and ‘z.’

additional_dict['x']=31
additional_dict['y']=42
additional_dict['z']=53

What happens if we look up the value for key ‘g’? It hasn’t been initialized yet.

print(additional_dict['g'])

As you can see, it gave it the default value we set using the defaultvalue function(). Now let’s have a look at the entire code.

def defaultvalue():
                return 0
additional_dict=defaultdict(defaultvalue)
additional_dict['x']=31
additional_dict['y']=42
additional_dict['z']=53
additional_dict['g']

Defaultdict

Defaultdict is a container similar to the dictionaries in module collections. A dictionary-like object is returned by Defaultdict, a subclass of the dict class. Except for that, defaultdict never generates a KeyError. Both dictionaries and defualtdict have nearly identical functionality. It assigns a default value to a key that doesn’t exist.

Syntax

defaultdict(default_factory)

Parameters

default_factory: A function that returns the dictionary’s default value. If this parameter is missing, the dictionary will throw a KeyError.

Example:

# program for demonstrating the defaultdict

from collections import defaultdict

# Function responsible for returning default values for keys not Available

def def_value():
	return "Not Available"

# Defininition of the dict
new_dict = defaultdict(def_value)
new_dict["x"] = 31
new_dict["y"] = 42

print(new_dict["x"])
print(new_dict["y"])
print(new_dict["z"])

defaultdict’s inner workings

In addition to the conventional dictionary operations, Defaultdict includes one writable instance variable and one method. The default_factory parameter is the instance variable, and the technique supplied is __missing__.

Default_factory: This is a function that returns the dictionary’s default value. If this parameter is missing, the dictionary will throw a KeyError.

# program for demonstrating the defaultdict's default_factory argument

from collections import defaultdict

# Defininition of the the dict and passing the lambda as the default_factory argument
dict_vals = defaultdict(lambda: "Not Available")
dict_vals["x"] = 31
dict_vals["y"] = 42

print(dict_vals["x"])
print(dict_vals["y"])
print(dict_vals["z"])

__missing__(): This function sets the dictionary’s default value. If default_factory is None, a KeyError is generated; otherwise, the function returns a default value for the specified key. When the specified key is not found, the dict class’s __getitem__() invokes this method. The value returned by the __missing__() method is raised or returned by __getitem__().

# program for demonstrating the defaultdict

from collections import defaultdict

# Defininition of the dict
dict_vals = defaultdict(lambda: "Not Available")
dict_vals ["x"] = 31
dict_vals ["y"] = 42

# Provision of the key's default value
print(dict_vals .__missing__('x'))
print(dict_vals .__missing__('z'))

Use of a List as a Default_factory

When the list class is specified as the default_factory option, a defaultdict containing list values is produced.

# program for demonstrating the defaultdict

from collections import defaultdict

# Defininition of a dict
new_dict = defaultdict(list)

for i in range(7):
	new_dict[i].append(i)

print(" dictionary with values as a list:")
print(new_dict)

Use of int as the default factory

When the int class is used as the default _actory argument, a defaultdict with a default value of zero is produced.

# program for demonstrating the defaultdict

from collections import defaultdict

# Definition of the dict
new_dict = defaultdict(int)

int_list = [11, 12, 13, 14, 12, 14, 11, 12]

# Iterating through the list for keeping the count
for i in int_list:
	# The default value is 0
	# so, it is needles entering the key first
	new_dic[i] += 1

print(new_dic)

defaultdict vs. dict()

In the module collections, Defaultdict is a container-like dictionary. The dictionary class has a subclass called Defaultdict that returns a dictionary-like object. Except that defaultdict never generates a KeyError, dictionaries, and defaultdict have nearly identical functionality. It assigns a default value to a key that doesn’t exist. You’re probably aware that if you try to append a value to a dictionary with a missing key, you’ll get a KeyError. For example:

employees = ["Bright", "Green", "Esther"]

ratings = [
    {"cleanliness": 50, "punctuality": 90, "performance": 70},
    {"cleanliness": 70, "punctuality": 85, "performance": 30},
    {"cleanliness": 60, "punctuality": 50, "performance": 60}
]

for index, val in enumerate(ratings):
    certificates[employees[index]].append(val)

To avoid the error, construct the following “if” statement:

for index, val in enumerate(grades):
    if employees[index] not in certificates:
        certificates[employees[index]] = []
    certificates[employees[index]].append(val)

Using defaultdict(), however, you can make your code the clearest as demonstrated below:

from collections import defaultdict

certificates = defaultdict(list)
for index, val in enumerate(ratings):
    certificates[employees[index]].append(val)

We set default_factory to list in the above example, and when we call missing key, we get an empty list [] as the default value of missing key. We can also use any data type, including int, str, dict, float, set, tuple, and list. Let’s put all of these data kinds to the test and see what happens:

int_test_dict = defaultdict(int)
float_test_dict = defaultdict(float)
dict_test_dict = defaultdict(dict)
dict_test_dict = defaultdict(list)
tuple_test_dict = defaultdict(tuple)
set_test_dict = defaultdict(set)
str_test_dict = defaultdict(str)

print(int_test_dict )
print(float_test_dict)
print(dict_test_dict)
print(tuple_test_dict )
print(set_test_dict )
print(str_test_dict )

You can also choose your default value. We need default_factory to be callable before we can move on to the next step:

lambda_test_dict = defaultdict(lambda: "use lambda in defining the default value")
lambda_test_dict[missing_key]

Alternatively, use the following code snippet:

def test_function():
    return "illustrating the usual functions default value"

lambda_test_dict = defaultdict(test_function)

However, if you don’t set default_factory, it defaults to None, and if you try to call the missing key, you’ll get a KeyError.

lambda_test_dict = defaultdict()
lambda_test_dict["missing_key"]

Both defaultdict and dict would be identical if they stored the same items, and this is tested by constructing one of each with the same things and a condition that evaluates if they’re equal, which will be true. If no arguments are supplied to defaultdict, it behaves similarly to a dictionary.

When using changeable collections as values in dictionaries, such as list, set, or dict, and initializing them before their first use, you can prevent encountering a KeyError. Using defaultdict, on the other hand, automates and simplifies this process. Therefore it may be recommended.

What is the difference between defaultdict and dict?

The critical distinction between defaultdict and dict is the implementation of three methods that are exclusive to defaultdict. (The rest of defaultdict’s methods are identical to those of its parent dict.)

  • .copy(), is responsible for supporting copy.copy().
  • .missing(key) is triggered when .getitem() finds the responsible, concerned key does not exist.
  • .default_factory, is tasked with storing the callable ._missing() invoked to assign default values to keys that do not exist automatically.

In addition, the procedure is found in dict. When a value for a non-existent key is required, use setdefault(). First, we specify the default value in defaultdict, which can be faster than using .setdefault()

employee_ratings = {"Jack": [85, 90], "Jill": [80, 95]}
employee_ratings["Jack"]
employee_ratings .get("Joe", [])
employee_ratings["Joe"]
employee_ratings .setdefault("Joe", [])
employee_ratings["Joe"]

from collections import defaultdict
new_dict = defaultdict(lambda: 34)
new_dict[8]
new_dict[8] += 61
new_dict[8]

Conclusion

You’ve learned about dictionaries and collections .defaultdict in this session. On the other hand, Dictionaries are a type of data structure that stores key-value pairs. It enables constant-time inserting and retrieval of key-value pairs. If the key does not exist in the dictionary, collections .defaultdict is a subclass of the dictionary data structure that provides for default values.

In contrast to a standard dictionary, we can see that a python defaultdict is pretty beneficial. It also has a straightforward syntax and allows us to specify a default value for uninitialized keys. It also informs the interpreter that we wish to work with a particular set of values.

You may also like

Leave a Comment