dict vs defaultdict
Quality Score
Overall Score: 8.1/10 ✅ Good
- Technical Accuracy: 28/35
- Code Quality: 20/25
- Educational Value: 20/25
- Documentation: 13/15
Last reviewed: June 22, 2026
Inspect the difference between a dict and a defaultdict in Python.
Access dict by key with square brackets
The easiest way to access a value inside dictionary is with [key] syntax.
But be careful, if the key does not exist, you will get a KeyError.
src.beginner.dict_vs_defaultdict.get_value_from_dict_with_square_brackets(my_dict, key)
Get value from a dict using square brackets.
This demonstrates direct dictionary access using square bracket notation. This approach raises a KeyError if the key doesn't exist, which can be useful when you want to ensure the key is present.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
my_dict
|
dict[str, str]
|
Dictionary with string keys and string values to search for the value. |
required |
key
|
str
|
Key to look up in the dictionary. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The value associated with the key in the dictionary. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If the key is not found in the dictionary. |
Source code in src/beginner/dict_vs_defaultdict/dict_vs_defaultdict.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | |
Access dict by key with get method
Using get method is another option. If key is not present in dict, None (or
custom value) is returned.
src.beginner.dict_vs_defaultdict.get_value_from_dict_with_get(my_dict, key, default=None)
Get value from a dict using get method.
The get() method provides a safe way to access dictionary values, returning a default value instead of raising KeyError when the key is not found. This is preferred when missing keys are expected.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
my_dict
|
dict[str, str]
|
Dictionary with string keys and string values to search for the value. |
required |
key
|
str
|
Key to look up in the dictionary. |
required |
default
|
str | None
|
Value to return if key is not found. Defaults to None. |
None
|
Returns:
| Type | Description |
|---|---|
str | None
|
The value associated with the key, or the default value if the |
str | None
|
key is not found. |
Source code in src/beginner/dict_vs_defaultdict/dict_vs_defaultdict.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | |
Use defaultdict
defaultdict enables a dict with a default value, even if requested with
square brackets. When setting defaultdict, you can send as first argument
(default_factory) a function that will be called when key is not present in
dict . String, int, list, None... any type you want. If you don't set
default_factory, KeyError will be raised if key is not present.
src.beginner.dict_vs_defaultdict.get_value_from_defaultdict(my_dict, key, default=None)
Get value from a defaultdict using square brackets.
Demonstrates how defaultdict automatically provides default values for missing keys. When a lambda function is passed to defaultdict, it's called to generate the default value whenever a missing key is accessed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
my_dict
|
dict[str, str]
|
Regular dictionary with string keys and string values to convert to defaultdict. |
required |
key
|
str
|
Key to look up in the defaultdict. |
required |
default
|
str | None
|
Value that the lambda function should return for missing keys. Defaults to None. |
None
|
Returns:
| Type | Description |
|---|---|
str | None
|
The value associated with the key, or the default value if the |
str | None
|
key was not in the original dictionary. |
Source code in src/beginner/dict_vs_defaultdict/dict_vs_defaultdict.py
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | |
Performance comparison
There is a great performance in using defaultdict vs get
from timeit import timeit
print("Get missing default dict:", timeit(
stmt="default_dict.get('key')",
setup="default_dict = {}",
number=5000000)
)
Get missing default dict: 0.1267744980000316
print("Get missing collection default dict:", timeit(
stmt="default_dict['key']",
setup="from collections import defaultdict; default_dict = defaultdict(lambda: None)", # noqa
number=5000000)
)
Get missing collection default dict: 0.0706390929999543
Common pitfalls
defaultdict creates a new entry in the dictionary when you access a missing
key. This can lead to unexpected behavior if you are not careful.