I have a function here that returns itself if a dictionary of dictionaries is detected (which will always be the case). I know that as of right now, my function runs until is errors out and hits a recursion depth limit. Would anyone know how I could properly pop or delete an item from the dictionary if k != numero
. I have tried popping k off of my_dict in the else statement but that produces an AttributeError saying strings do not have that ability. I ran the code through pycharm debugger and it will show 'four'
as k
and 'five'
as v. Both of these are strings but they are present key/values in the dict so I am confused.
Here is my code.
my_dict = {'one': {'two': 'three', 'four': 'five', 'six': 'seven'}}
k = ''
numero = 'six'
def find_location(d):
global key
for k, v in d.iteritems():
if isinstance(v, dict):
key = find_location(v)
elif k == numero:
print v
print k
else:
return find_location(d)