Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,31 +1,37 @@
---
Title: 'defaultdict'
Description: 'Returns a dictionary-like object.'
Description: 'Creates a dictionary-like object that provides default values for missing keys.'
Subjects:
- 'Computer Science'
- 'Data Science'
Tags:
- 'Modules'
- 'Dictionaries'
- 'Data Types'
- 'Dictionaries'
CatalogContent:
- 'learn-python-3'
- 'paths/computer-science'
---

In Python, **defaultdict** is a data type that belongs to the [`collections`](https://www.codecademy.com/resources/docs/python/collections-module) module. It is a [dictionary](https://www.codecademy.com/resources/docs/python/dictionaries) subclass that is used to return a dictionary-like object.
In Python, **`defaultdict`** is a data type that belongs to the [`collections`](https://www.codecademy.com/resources/docs/python/collections-module) module. It is a [dictionary](https://www.codecademy.com/resources/docs/python/dictionaries) subclass that automatically provides a default value for missing keys.

## Syntax

```pseudo
collections.defaultdict(default_factory)
```

- `default_factory`: It gives the default value for the dictionary object.
**Parameters:**

- `default_factory`: A callable (such as a function or type like `int`, `list`, `set`) that provides the default value for missing keys. If set to `None`, a `KeyError` is raised when accessing a missing key.

**Return value:**

## Example
Returns a `defaultdict` object, which behaves like a dictionary but creates default values for missing keys using the specified `default_factory`.

The following example demonstrates the `defaultdict` data type:
## Example 1: Using a Custom Function

The following example demonstrates the `defaultdict` data type with a custom function as `default_factory` argument:

```py
from collections import defaultdict
Expand All @@ -51,6 +57,60 @@ Here is the output for the above code:
Not Declared
```

Example 2: Using Built-in Callables

This example demonstrates `defaultdict` with built-in types (`int`, `list`, `str`, `set`) as the `default_factory`:

```py
from collections import defaultdict

intDefaultDict = defaultdict(int)
listDefaultDict = defaultdict(list)
strDefaultDict = defaultdict(str)
setDefaultDict = defaultdict(set)

print(intDefaultDict[0])
print(listDefaultDict['zero'])
print(strDefaultDict['0'])
print(setDefaultDict['a'])
```

Here is the output of the above code:

```shell
0
[]

set()
```

Example 3: Working with Lists

This example shows how `list` as `default_factory` allows appending to keys that don’t yet exist:

```py
from collections import defaultdict

myDefaultDict = defaultdict(list)

myDefaultDict['apple'].append(1)
# myDefaultDict['apple'] does not exist so it defaults to empty list [],
# then 1 is appended to it.

myDefaultDict['orange'] = 2
#The empty list [] is replaced by integer 2 here.

print(myDefaultDict['apple'])
print(myDefaultDict['orange'])
```

Here is the output of the above code:

```shell
[1]
2
```

## Codebyte Example

Run the following codeblock and explore more about the `defaultdict` data type:
Expand Down