Skip to main content

DefaultDict

This is a package for a defaultdict that deep copies its default value.

This can be used to implement a simple counter object, similar to the example code this is based on.

counter = DefaultDict()

for item in ['spam', 'egg', 'spam', 'spam', 'bacon', 'spam']:
    counter[item] += 1

print("Menu contents:")
for item in counter:
    print("  {}x {}".format(counter[item], item))

Output:

Menu contents:
  1x bacon
  1x egg
  4x spam

Unlike collections.defaultdict, the default value is an object - not a factory. This allows for easier creation of nested data structures. For example, the following is valid and encouraged:

# shapes_by_color[color][shape] = count
shapes_by_color = DefaultDict(
    default=DefaultDict(
        {'total': 0},
        default=0
    )
)

with open('test/shapes.log', 'r') as fp:
    for line in fp:
        color, shape = line.split()
        shapes_by_color[color][shape] += 1
        shapes_by_color[color]['total'] += 1

for color, shapes in shapes_by_color.items():
    print("{}:".format(color))
    for shape, count in shapes.items():
        if shape == 'total':
            continue
        print("  {}x {}".format(count, shape))
    print("  Total: {}".format(count))

Output:

green:
  2x triangle
  1x circle
  Total: 3
red:
  1x octagon
  2x rhombus
  Total: 3
blue:
  1x triangle
  1x square
  Total: 2

Note that the default value is a reference, and is affected by external changes. This only affects new uses of the default value, not previous uses.

spam = DefaultDict(default=['Hello'])
print(spam['eggs'])

spam.default.append('world')
print(spam['sausage'])

print(spam)

Output:

['Hello']
['Hello', 'world']
DictWithDefault(init={'sausage': ['Hello', 'world'], 'eggs': ['Hello']}, default=['Hello', 'world'])

Release files for defaultdict-NickNackGus 1.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for defaultdict-NickNackGus 1.0.2
File Size Uploaded
defaultdict-NickNackGus-1.0.2.tar.gz 2.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for defaultdict-NickNackGus 1.0.2
File Interpreter ABI Platform
defaultdict_NickNackGus-1.0.2-py3-none-any.whl Python 3 none any Details

Total release size:6.4 kB

Release files / defaultdict-NickNackGus-1.0.2.tar.gz

Download URL defaultdict-NickNackGus-1.0.2.tar.gz
Size 2.6 kB
Tags Source
SHA-256 checksum
How to use checksums
323264f95714ee0a6c4f2aae255e0199bea41cc1507a948850d90f3fd963865f
BLAKE2b-256 checksum
How to use checksums
d83451ccbb2caa6aa7bda7153fbe5c5a4b3f8451cb8c93480fcde28f0b0f6cde
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.9.1 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.5.2

Release files / defaultdict_NickNackGus-1.0.2-py3-none-any.whl

Download URL defaultdict_NickNackGus-1.0.2-py3-none-any.whl
Size 3.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d2f3cbaffc15df595ade7160651939eeab112647d1a2a0b72acf655c5f9a1cc1
BLAKE2b-256 checksum
How to use checksums
d3be6aa0c0ffcfdf6777ad36ec82d52a613704ca707ba727cd69301b60a50458
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.9.1 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.5.2

Release history Release notifications | RSS feed

This release

1.0.2 This release

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page