I want to find all instances of str
in any nested data structure of dicts and lists. Not all terminal items will be str
.
A data example:
data = {'iso_seq_bams': [['5/X/tmp2oWhu5.tmp', 'y/H/tmp6Po0_X.tmp']],
'annotation': None,
'bams': {'BAM': {'ERR579132Aligned.sortedByCoord.out.bam': ['Y/o/tmpntzREn.tmp', 'z/c/tmp6DmQhS.tmp']},
'INTRONBAM': {}}}
And the result expected would thus be ['5/X/tmp2oWhu5.tmp', 'y/H/tmp6Po0_X.tmp', 'Y/o/tmpntzREn.tmp', 'z/c/tmp6DmQhS.tmp']
I have attempted to implement this in a recursive fashion, but it doesn't seem to work. The result is an empty list, currently.
def descend_object(obj):
if isinstance(obj, dict):
for item in obj.values():
descend_object(item)
elif isinstance(obj, list):
for item in obj:
descend_object(item)
elif isinstance(obj, str):
yield obj