Skip to main content

Access nested dict elements as d[[1,2,3]] instead of d[1][2][3] - compatible with dict() - no requirements

pip install mymulti-key-dict

Tested against Windows 10 / Python 3.10 / Anaconda

The MultiKeyDict class is a special type of dictionary that allows you to use lists as keys to access and modify nested elements within the dictionary. This feature sets it apart from regular dictionaries and provides additional flexibility in working with hierarchical data structures.

Using lists as keys in the MultiKeyDict allows you to traverse multiple levels of nesting in a single operation. For example, if you have a nested dictionary d and you want to access the value at d[1][2][3], you can simply pass the list [1, 2, 3] as the key ( d[[1, 2, 3]] ) to retrieve the desired value. This makes it easier to work with complex nested structures and eliminates the need for multiple indexing operations.

Furthermore, the MultiKeyDict class provides methods for converting the nested dictionary to a normal dictionary, making a deep copy of the dictionary, retrieving items, keys, and values, updating the dictionary with another dictionary, clearing the dictionary, and more. These methods make it convenient to perform common dictionary operations while preserving the nested structure.

By extending the dict class, the MultiKeyDict class inherits the basic dictionary functionality and adds the ability to handle nested keys. It also overrides certain methods, such as __getitem__, __setitem__, __delitem__, and others, to enable the list key functionality and provide the expected behavior for accessing and modifying nested elements.

from mymulti_key_dict import MultiKeyDict
dict2 = {2: {"c": 222}, 3: {"d": {3, 6}}}
d = MultiKeyDict(dict2)

d[[1, 3, 4, 5, 67]] = 100
print(d[[1, 3]])
dd = {2: {"c": 222}, 3: {"d": {3, 6}}}
print(f"{list(d)=}")
print(f"{len(d)=}")
print(f"{d[1]=}")
print(f"{d[1][3]=}")
print(f"{d[[1,3]]=}")
d[[23, 4, 5, 323]] = "x"
print(f"""d[[23,4,5,323]] = 'x'={d}""")
print(f"{23 in d=}")
del d[[1, 3]]
print(f"""del d[[1,3]]={d}""")
del d[1]
print(f"""del d[1]={d}""")
di2 = d.copy()
print(f"{di2 == d=}")
print(f"{di2 is d=}")
di2.clear()
print(f"""di2.clear()={di2}""")
print(f"{list(iter(d))=}")
print(f"{d.get(2)=}")
print(f"{d.get([23,4,5])=}")
print(f"{d.items()=}")
print(f"{d.keys()=}")
print(f"{d.pop(3)=}")
print(f"{d.pop([23,4,5])=}")
print(f"""{d.popitem()=}""")
print(f"""after d.popitem={d}""")
dict2 = {2: {"c": 222}, 3: {"d": {3, 6}}, 4: 3, 33: {33: 2}}
d = MultiKeyDict(dict2)
print(f"""{list(d.reversed())=}""")
d.update({4: {44: 4}})
print(f"""d.update...={d}""")
d5 = d | {3: 4}
d |= {3: 4}
print(f"""d |= {{3:4}}={d}""")
print(f'{d.to_dict()=}')





{4: {5: {67: 100}}}
list(d)=[2, 3, 1]
len(d)=3
d[1]={3: {4: {5: {67: 100}}}}
d[1][3]={4: {5: {67: 100}}}
d[[1,3]]={4: {5: {67: 100}}}
d[[23,4,5,323]] = 'x'={1: {3: {4: {5: {67: 100}}}},
 2: {'c': 222},
 3: {'d': {3,
           6}},
 23: {4: {5: {323: 'x'}}}}
23 in d=True
del d[[1,3]]={1: {},
 2: {'c': 222},
 3: {'d': {3,
           6}},
 23: {4: {5: {323: 'x'}}}}
del d[1]={2: {'c': 222},
 3: {'d': {3,
           6}},
 23: {4: {5: {323: 'x'}}}}
di2 == d=True
di2 is d=False
di2.clear()={}
list(iter(d))=[2, 3, 23]
d.get(2)={'c': 222}
d.get([23,4,5])={323: 'x'}
d.items()=dict_items([(2, {'c': 222}), (3, {'d': {3, 6}}), (23, {4: {5: {323: 'x'}}})])
d.keys()=dict_keys([2, 3, 23])
d.pop(3)={'d': {3, 6}}
d.pop([23,4,5])={323: 'x'}
d.popitem()=(2, {'c': 222})
after d.popitem={23: {4: {}}}
list(d.reversed())=[33, 4, 3, 2]
d.update...={2: {'c': 222},
 3: {'d': {3,
           6}},
 4: {44: 4},
 33: {33: 2}}
d |= {3:4}={2: {'c': 222},
 3: 4,
 4: {44: 4},
 33: {33: 2}}
d.to_dict()={2: {'c': 222}, 3: 4, 4: {44: 4}, 33: {33: 2}}

Metadata

Release files for mymulti-key-dict 0.15

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

Source distribution (sdist)

Source distribution for mymulti-key-dict 0.15
File Size Uploaded Provenance Actions
mymulti_key_dict-0.15.tar.gz 4.8 kB Without provenance Details for mymulti_key_dict-0.15.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for mymulti-key-dict 0.15
File Interpreter ABI Platform Actions
mymulti_key_dict-0.15-py3-none-any.whl Python 3 none any Details for mymulti_key_dict-0.15-py3-none-any.whl

Total release size: 11.5 kB

Release files / mymulti_key_dict-0.15.tar.gz

Download URL mymulti_key_dict-0.15.tar.gz
Size 4.8 kB
Tags Source
SHA-256 checksum
How to use checksums
ba4f5040aa66a90f576036c4798f9c0ca8e0641a75b98911ab637a1995296b1b
BLAKE2b-256 checksum
How to use checksums
c30aaa0f08c8dceffa9238175f504c1411a8fd78f832e3a974e96d195261a466
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.10

Release files / mymulti_key_dict-0.15-py3-none-any.whl

Download URL mymulti_key_dict-0.15-py3-none-any.whl
Size 6.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
418c518c1cafe7f72d3392d83c43a02cead0ba76de331beb0d7f1d566abd60d5
BLAKE2b-256 checksum
How to use checksums
fb790b118139cb8daa298073787a123d471d379ca7a16ee93f7eb311151dd5b3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.10

Security for mymulti-key-dict 0.15

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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