On this page
collections.defaultdict
Prev: collections.Counter | Up: collections | Next: collections.deque
Rel: collections.ChainMap, collections.Counter, collections.deque, collections.namedtuple
Ref: docs.python.org — collections.defaultdict
Tags: #public #python3 #stdlib #collections
Import
import collections
from collections import defaultdict
Signature & Overview
collections.defaultdict(default_factory=None, /, [...]) --> dict with default factory
The default factory is called without arguments to produce a new value when a key is not present, in getitem only.
Methods & Attributes
defaultdict.clear(self, /)— Remove all items from the dict.defaultdict.copy(self, /)— D.copy() -> a shallow copy of D.defaultdict.fromkeys(iterable, value=None, /)— Create a new dictionary with keys from iterable and values set to value.defaultdict.get(self, key, default=None, /)— Return the value for key if key is in the dictionary, else default.defaultdict.items(self, /)— Return a set-like object providing a view on the dict's items.defaultdict.keys(self, /)— Return a set-like object providing a view on the dict's keys.defaultdict.pop(...)— If the key is not found, return the default if given; otherwise, raise a KeyError.defaultdict.popitem(self, /)— Remove and return a (key, value) pair as a 2-tuple.defaultdict.setdefault(self, key, default=None, /)— Insert key with a value of default if key is not in the dictionary.defaultdict.update(...)— If E is present and has a .keys() method, then does: for k in E.keys(): D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]defaultdict.values(self, /)— Return an object providing a view on the dict's values.
Usage
from collections import defaultdict
groups: defaultdict[str, list[int]] = defaultdict(list)
for key, val in [("a", 1), ("b", 2), ("a", 3)]:
groups[key].append(val)
assert dict(groups) == {"a": [1, 3], "b": [2]}
Gotchas
- Reading a missing key mutates the dict: Accessing
groups["missing"]inserts an empty default value—usegroups.get("missing")or"missing" in groupsto check without inserting. - Pass a zero-arg callable, not a value: Write
defaultdict(list)ordefaultdict(lambda: 42), notdefaultdict([]).
REPL Inspect
from collections import defaultdict
help(defaultdict)
Linked from
- tuts/python3/stdlib_module/collections
- tuts/python3/stdlib_module/collections/ChainMap
- tuts/python3/stdlib_module/collections/Counter
- tuts/python3/stdlib_module/collections/deque
- tuts/python3/stdlib_module/collections/namedtuple
- tuts/python3/stdlib_module/collections/OrderedDict
- tuts/python3/stdlib_module/collections/UserDict
- tuts/python3/stdlib_module/collections/UserList
- tuts/python3/stdlib_module/collections/UserString
Related notes
- tuts/python3/stdlib_module/collections/ChainMap Linked here; links to this page; shared tags: #collections, #stdlib
- tuts/python3/stdlib_module/collections/Counter Linked here; links to this page; shared tags: #collections, #stdlib
- tuts/python3/stdlib_module/collections/deque Linked here; links to this page; shared tags: #collections, #stdlib
- tuts/python3/stdlib_module/collections/namedtuple Linked here; links to this page; shared tags: #collections, #stdlib
- tuts/python3/stdlib_module/collections Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/collections/OrderedDict links to this page; shared tags: #collections, #stdlib
- tuts/python3/stdlib_module/collections/UserDict links to this page; shared tags: #collections, #stdlib
- tuts/python3/stdlib_module/collections/UserList links to this page; shared tags: #collections, #stdlib
- tuts/python3/stdlib_module/collections/UserString links to this page; shared tags: #collections, #stdlib
- tuts/rust/std/collections shared tags: #collections, #stdlib