[Python-Dev] Proposal: defaultdict (original) (raw)

Raymond Hettinger python at rcn.com
Fri Feb 17 05:27:17 CET 2006


Over lunch with Alex Martelli, he proposed that a subclass of dict with this behavior (but implemented in C) would be a good addition to the language

I would like to add something like this to the collections module, but a PEP is probably needed to deal with issues like:

bag like behavior

dd = collections.default_dict() dd.default(0) for elem in collection: dd[elem] += 1

setdefault-like behavior

dd = collections.default_dict() dd.default(list) # instantiate a new list for empty cells for page_number, page in enumerate(book): for word in page.split(): dd[word].append(word)

Raymond



More information about the Python-Dev mailing list