Introduction
In Python, dictionaries are one of the versatile built-in information sorts. They’re nice for structuring information and may help you clear up a myriad of issues. However what if I instructed you there is a extra highly effective model of dictionaries that you simply may not have heard of? Yep, I am speaking concerning the collections.defaultdict
kind.
On this article, we’ll discover what a defaultdict
is and the way it differs from an everyday Python dictionary (or dict
for brief).
What’s a dict?
A dict
, or dictionary, is a built-in Python information kind that shops mutable, unordered collections of key-value pairs. Every key-value pair within the dictionary maps the important thing to its related worth, making it simpler to retrieve values for particular keys.
scholar = {
"identify": "John Doe",
"age": 20,
"programs": ["Math", "Science"]
}
print(scholar["name"])
On this code, the keys are “identify”, “age”, and “programs”, they usually every have related values. You possibly can entry any worth by its key, as we did with scholar["name"]
.
However what occurs if you attempt to entry a key that does not exist within the dictionary? Properly, Python throws a KeyError
:
print(scholar["grade"])
This is without doubt one of the limitations of a traditional dictionary. It does not deal with lacking keys very nicely. In real-world functions, this could trigger your program to crash when you’re not cautious. That is the place defaultdicts
come into play, however we’ll get into that later within the article.
Word: You possibly can keep away from KeyError
exceptions in regular dictionaries by utilizing the get
methodology, which returns None
if the bottom line is not discovered. Nonetheless, this is not all the time ultimate, particularly if you wish to present a default worth apart from None
.
print(scholar.get("grade"))
What’s a defaultdict?
A defaultdict
is a specialised dictionary supplied by the collections
module in Python. It is a subclass of the built-in dict
class. So, what makes it so particular? Properly, it does not throw a KeyError
if you attempt to entry or modify keys that are not really within the dictionary. As an alternative, it initializes it with a component of the information kind that you simply go as an argument on the creation of defaultdict
. This may be very helpful if you’re working with giant information constructions.
Let’s take a fast have a look at how you’d initialize a defaultdict
:
from collections import defaultdict
dd = defaultdict(listing)
Within the instance above, when you attempt to entry a key that does not exist, Python will return an empty listing []
as a substitute of throwing a KeyError
.
print(dd["non_existent_key"])
The argument you go whereas initializing defaultdict
is named default_factory
. It is a perform that gives the default worth for the dictionary created. If this argument is absent, then the defaultdict
basically behaves like a traditional dict
.
Key Variations
Now that we perceive what a defaultdict
is, let’s check out the important thing variations between a defaultdict
and a typical Python dictionary.
- Default Values: Probably the most important distinction, as we have already seen, is that
defaultdict
robotically assigns a default worth to a non-existent key. That is in several than a regulardict
, which raises aKeyError
if you attempt to entry or modify a non-existent key.
d = {}
print(d["non_existent_key"])
from collections import defaultdict
dd = defaultdict(int)
print(dd["non_existent_key"])
- Initialization: Whereas initializing a
defaultdict
, you might want to present adefault_factory
perform which can resolve the default worth for the non-existent keys. Then again, a regulardict
does not require or assist this.
d = {}
dd = defaultdict(listing)
- Use Instances:
defaultdict
is extra helpful if you’re coping with giant information units the place you wish to keep away from dealing withKeyError
exceptions. It is generally used for grouping, counting, or accumulating operations.
When to Use defaultdict vs dict
In fact, the selection between defaultdict
and dict
will depend on your particular wants. In case you’re coping with a state of affairs the place you wish to keep away from key errors and you understand prematurely what sort of default worth you’d need for non-existing keys, defaultdict
is the way in which to go.
As an example you are constructing a dictionary to depend the frequency of phrases in a textual content. With a traditional dictionary, you’d should examine if a phrase is already a key within the dictionary earlier than incrementing its depend. With defaultdict
, you’ll be able to merely set the default worth kind as int
and increment the depend with none checks.
Then again, if you need your program to throw an error when a non-existent secret is accessed, or if you do not have a transparent default worth, an everyday dict
could also be extra appropriate.
Find out how to Use defaultdict
Utilizing defaultdict
is kind of easy. You begin by importing it from the collections
module. Then, if you create a defaultdict
, you go within the default kind for the dictionary. This may very well be int
, listing
, set
, dict
, or perhaps a user-defined perform.
Let’s check out an instance. Suppose we wish to create a dictionary to retailer the grades of scholars in several topics. We are able to use a defaultdict
with an inventory because the default kind:
Take a look at our hands-on, sensible information to studying Git, with best-practices, industry-accepted requirements, and included cheat sheet. Cease Googling Git instructions and truly be taught it!
from collections import defaultdict
grades = defaultdict(listing)
grades['Math'].append(85)
grades['English'].append(90)
print(grades)
While you run this code, you may get the next output:
defaultdict(<class 'listing'>, {'Math': [85], 'English': [90]})
As you’ll be able to see, we did not should examine if “Math” or “English” had been already keys within the dictionary. We had been capable of immediately append the grades. If we attempt to entry the grades for a topic that hasn’t been added but, we’ll get an empty listing as a substitute of a key error:
print(grades['Science'])
This can output:
[]
Word: Keep in mind that the default kind you go to defaultdict
is a perform, not a worth. So, you need to go listing
as a substitute of []
, or int
as a substitute of 0
.
Find out how to Use dict
The Python dict
is a built-in information kind used to retailer information in key-value pairs. This is a easy instance of methods to use it:
my_dict = {'identify': 'John', 'age': 30}
print(my_dict['name'])
my_dict['age'] = 31
print(my_dict['age'])
my_dict['job'] = 'Engineer'
print(my_dict)
One factor to recollect when utilizing dict
is that it’s going to increase a KeyError
when you attempt to entry a key that does not exist:
print(my_dict['hobby'])
Word: To keep away from this, you should use the get()
methodology, which returns None
or a default worth of your selection if the important thing does not exist.
print(my_dict.get('interest'))
print(my_dict.get('interest', 'default'))
Conclusion
On this article, we have taken a deeper dive into the world of Python dictionaries, focusing particularly on the dict
and collections.defaultdict
sorts. We have explored their key variations, like how defaultdict
gives a default worth for non-existent keys, thus avoiding KeyError
exceptions. We have additionally checked out their use-cases, with dict
being higher for situations the place you might want to strictly management what keys exist in your dictionary, and defaultdict
being extra helpful if you’re coping with giant datasets and must keep away from fixed key existence checks.