I have dictionaries I have to iterate like this:
for akey,bdict in cdict.items():
for dkey,edict in bdict.items():
for fkey,gdict in edict.items():
for hkey,ival in gdict.items():
# do something
How can I define explode
so that I can do something like this with an arbitrarily large nested dict (I might need parentheses around some of the iterated items, but use of parentheses will be determined by explode
, I would imagine)?
for akey,dkey,fkey,hkey,ival in explode(cdict):
# do something
# or
for *keys,val in explode(any_nesteddict):
# do something
updated after answer from popcorndude on Jul 9 at 23:38
Is there a way to unpack to a certain level in the dictionary eg if I wanted to get akey
, dkey
, and edict
?
for (akey,dkey),edict in explode(cdict, level=2):
# do stuff
# which is different from original question:
for (akey,dkey,fkey,hkey),ival in explode(cdict):
# do stuff