Python Defaultdict

In Python, a dictionary represents an unordered collection of data values that can be used to store data values in the same way a map can. The dictionary has a key-value pair, unlike other data types with a single value as an element. The key must be unique and unchanging, according to Dictionary. It means that a Python Tuple, unlike a Python List, can be used as a key.

A dictionary is built by enclosing a series of entries in curly braces and separating them with a comma. Now let’s see what happens if we add a defaultdict. Note that you must import the defaultdict from the collections library.

from collections import defaultdict

defaultdict_demo = defaultdict(int)
print(defaultdict_demo[3])

The defaultdict has been given the datatype int in this example. As a result, unless a value is defined for every key that does not already exist in defaultdict_demo, it will be assigned a value of 0.

What more qualities may it possess?

Let’s examine what happens with the other two options now that we’ve seen how a defaultdict works and what occurs when it’s given an int argument.

Using the parameter set as a starting point

As a result, a dictionary of sets is created. As a result, they are assigned to the key as a set of values because the same key has two different values. Three is an empty key. Thus it gets an empty set.

from collections import defaultdict

defaultdict_vals = defaultdict(set)

defaultdict_vals['one'].add(1)
defaultdict_vals['two'].add(2)
defaultdict_vals['one'].add('1')
defaultdict_vals['three']

print(dict(defaultdict_vals.items()))

Using a list as a parameter is a good idea.

As a result, a dictionary of lists is created. To demonstrate the difference, we utilize the same example as before. There are two of them. First, a key with no value is entered is given an empty list. Furthermore, all values are now saved as a list. Second, we may use the append function, which is used for lists, to add elements to the defaultdict.

rom collections import defaultdict

defaultdict_vals = defaultdict(list)

defaultdict_vals['one'].append(1)
defaultdict_vals['two'].append(2)
defaultdict_vals['one'].append('1')
defaultdict_vals['three']

print(dict(defaultdict_demo.items()))

Example: a program for demonstrating a dictionary in Python

dict_val = {1: 'Code', 2: 'under', 3: 'scored', 4: 'demo'}
print("The dictionary is:", dict_val)
print(dict_val[1])

# When you uncomment this print(dict_val[5])
# will raise a KeyError as the
# 5 is not present in the dictionary

It is possible that when the KeyError is raised, it will cause a problem. To deal with this, Python provides a dictionary-like container called Defaultdict, which is included in the collections module.

DefaultDict

Defaultdict is a container similar to the dictionaries in module collections. The dictionary class has a subclass called Defaultdict that returns a dictionary-like object. Except for 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.

defaultdict(default_factory) is the syntax.

Parameters:

default _factory: 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

from collections import defaultdict


# Function for returning  designated default values for keys not present

def check_value():
	return "Value is not Present"
	
# dict definition
new_dict = defaultdict(check_value)
new_dict["x"] = 1
new_dict["y"] = 2

print(new_dict["a"])
print(new_dict["b"])
print(new_dict["c"])

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 method 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 default_factory argument of the defaultdict

from collections import defaultdict

	
# definition and passing of the dict
# default_factory argument is the  lambda
dict_val = defaultdict(lambda: "value not available")
dict_val["a"] = 1
dict_val["b"] = 2

print(dict_val["a"])
print(dict_val["b"])
print(dict_val["c"])

__missing__(): This function sets the dictionary’s default value. If the 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__() method invokes this method. The value returned by the __missing__() method is raised or returned by __getitem__().

Example: a program for demonstrating the defaultdict in Python

from collections import defaultdict

	
#  dict definition
dict_val = defaultdict(lambda: "value is not available")
dict_val["a"] = 1
dict_val["b"] = 2

# provision of the default value for the key
print(dict_val.__missing__('a'))
print(dict_val.__missing__('d'))

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

Example: a program for demonstrating the use of defaultdict in Python

from collections import defaultdict


# Defining a dict
d = defaultdict(list)

for i in range(5):
	d[i].append(i)
	
print("Dictionary with values as list:")
print(d)

What makes defaultdict unique?

The dict class has a subclass called defaultdict. Its significance stems from the fact that it permits a default value to be assigned to each new key depending on the type of dictionary formed.

A defaultdict is generated by passing an argument to its definition that can be one of three types: list, set, or int. The dictionary is built according to the supplied data type, and any key that does not exist in the defaultdict is assigned a default value rather than returning a KeyError.

The first code snippet below shows a simple dictionary and how it throws an error when a key is accessed that does not exist in the dict.

dict_samp = dict()
print(dict_samp[2])

Use the default_factory as int

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

# program for illustrating the defaultdict


from collections import defaultdict


# definition of the dict
dict_val = defaultdict(int)

num_list = [1, 2, 3, 4, 2, 4, 1, 2]

# Iterating through the list responsible for keeping the count
for i in num_list:
	
	# The default value is 0, thus, no need entering the key first
	dict_val[i] += 1
	
print(dict_val)

Python defaultdict is used to deal with missing keys

In this lesson, you’ll learn how to use the Python defaultdict to deal with missing keys. You already know that when you establish a new defaultdict, you must specify a new default value type. When attempting to retrieve a missing key, this allows us to manage missing data. Let’s have a peek at what this looks like:

# how to deal with missing data with defaultdict
from collections import defaultdict
default = defaultdict(int)

print(default['Name'])

# Returns: 0

We can see from the preceding example that we could access a key that didn’t exist and be given the value of 0. The callables for int, float, list and set can be passed in. The defaultdict object counts items in a list in the next section.

Using Python defaultdict, count the number of items in a list

The ability to count items in an iterable and return the counts in a dictionary is one of the more inventive uses for the defaultdict object. First, let’s explore how we can do this with a conventional dictionary before diving into how to do it with the defaultdict object.

# Counting Items in a List
user_names = ['Mike', 'Joy', 'Cate', 'Bright', 'Angie', 'Winny', 'Ester', 'Man']

counts = {}
for name in user_names:
    if name in counts:
        counts[name] += 1
    else:
        counts[name] = 1

print(counts)

We need to insert the if-else statement in the code above to account for the circumstance where the key does not already exist in our dictionary. We can greatly simplify this by using the defaultdict object. We can use an integer as our default key and securely increment our keys, even if they do not exist. Let’s have a look at how this appears:

# Count the number of Items in the given List using the defaultdict
from collections import defaultdict
user_names = ['Mike', 'Joy', 'Cate', 'Bright', 'Angie', 'Winny', 'Ester', 'Man']

count_val = defaultdict(int)
for val in user_names:
    count_val[val] += 1

print(count_val)

In this case, we created a defaultdict object and passed it into the callable as an integer value. It indicates that if a key isn’t found, the value will be set to 0. As a result, if we come across a key that doesn’t exist, we can safely increase its value by one. This code is far cleaner than our previous naive implementation, which used a standard dictionary.

Python defaultdict can be used to group data

We can organize data using the defaultdict object based on different data structures. We can use this to iterate over a list of tuples, another dictionary, or a set of lists to group data in meaningful ways. It is a lot easier with defaultdict. We can delete the if-else statement since we can specify a default value as an empty list. Let’s explore how it works in practice:

# We need to group the Items using dictionaries with defaultdict
from collections import defaultdict
user_vals = {'Mike': 'Los Angeles', 'Joy': 'London', 'Bright': 'Manchester', 'Winny': 'Toronto', 'Man': 'London'}
locations = defaultdict(list)

for user, loc in user_vals.items():
    locations[loc].append(user)

print(loc)

Conclusion

In this tutorial, you’ve learned about the Python defaultdict objects, which are part of the collections library. Upon accessing a key that doesn’t exist, the object overrides the typical dictionary behavior of throwing a KeyError. Overall, the Python dictionary, dict, is a collection of terms and their definitions and key-value pairs of any data type. The built-in dict class has a subdivision called defaultdict.

You’ve also learned all there is to know about the defaultdict and how it differs from the standard Python dictionary. Additionally, we’ve looked at how to deal with missing errors in Python in general. Finally, you’ve discovered how to work with the defaultdict object and count, group, and accumulate data.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *