I have a code that uses the NestedDict class here: How can I access a deeply nested dictionary using tuples?. "I have a fully working example based on @JCash's answer:"
There are two things I would like to do with it that are causing me trouble. First, I would like to delete one of it's elements if the value of that element is zero. second, if an element is an empty dictionary because all the entries of that dictionary were deleted, I would like to delete the empty dictionary.
Using the class sited above, an example is the following:
my_tuple = (0, 1, 0, 0, 0, 1, 0, 0)
d = NestedDict()
print d
d[my_tuple] = 4
print d
#del d[my_tuple]
del d[0][1][0][0][0][1][0][0]
del d[0][1][0][0][0][1][0]
del d[0][1][0][0][0][1]
del d[0][1][0][0][0]
del d[0][1][0][0]
del d[0][1][0]
del d[0][1]
del d[0]
print d
The long list of del's is necessary in order to get rid of the multiple levels of nesting. the commented out del statement (which gives a key-error) is what I would like to implement, with a tuple of arbitrary length.
Deleting the intermediate levels shouldn't be hard once I figure out how to delete the first. I already know what I want to delete, and I can test for empty dictionaries with: if (dictionary entry) == {})
Any ideas?
Edit: Output is:
{}
{0: {1: {0: {0: {0: {1: {0: {0: 4}}}}}}}}
{}