def recursive_iterator(iterable):
for item in iterable:
# directly iterable types:
if type(item) in (list, tuple, set, frozenset):
for child_item in recursive_iterator(item):
yield child_item
# other iterable types where we do not want to iterate over the item directly:
elif type(item) in (dict,):
for child_item in recursive_iterator(item.values()):
yield child_item
# not iterable types which we want to return as they are:
else:
yield item
f = (3, 4, 5, {3: 4}, [16, 7, 8])
g = (1, 2, [3, 4, [5, 6], {7: 8}], 9, 10, {11: f}, {12: [1, 2, {3, 4}, [5, 6]]})
for x in recursive_iterator(g):
print(x, end=" ")
ZGVmIHJlY3Vyc2l2ZV9pdGVyYXRvcihpdGVyYWJsZSk6CiAgICBmb3IgaXRlbSBpbiBpdGVyYWJsZToKCiAgICAgICAgIyBkaXJlY3RseSBpdGVyYWJsZSB0eXBlczoKICAgICAgICBpZiB0eXBlKGl0ZW0pIGluIChsaXN0LCB0dXBsZSwgc2V0LCBmcm96ZW5zZXQpOgogICAgICAgICAgICBmb3IgY2hpbGRfaXRlbSBpbiByZWN1cnNpdmVfaXRlcmF0b3IoaXRlbSk6CiAgICAgICAgICAgICAgICB5aWVsZCBjaGlsZF9pdGVtCgogICAgICAgICMgb3RoZXIgaXRlcmFibGUgdHlwZXMgd2hlcmUgd2UgZG8gbm90IHdhbnQgdG8gaXRlcmF0ZSBvdmVyIHRoZSBpdGVtIGRpcmVjdGx5OgogICAgICAgIGVsaWYgdHlwZShpdGVtKSBpbiAoZGljdCwpOgogICAgICAgICAgICBmb3IgY2hpbGRfaXRlbSBpbiByZWN1cnNpdmVfaXRlcmF0b3IoaXRlbS52YWx1ZXMoKSk6CiAgICAgICAgICAgICAgICB5aWVsZCBjaGlsZF9pdGVtCgogICAgICAgICMgbm90IGl0ZXJhYmxlIHR5cGVzIHdoaWNoIHdlIHdhbnQgdG8gcmV0dXJuIGFzIHRoZXkgYXJlOgogICAgICAgIGVsc2U6IAogICAgICAgICAgICB5aWVsZCBpdGVtCgoKZiA9ICgzLCA0LCA1LCB7MzogNH0sIFsxNiwgNywgOF0pCmcgPSAoMSwgMiwgWzMsIDQsIFs1LCA2XSwgezc6IDh9XSwgOSwgMTAsIHsxMTogZn0sIHsxMjogWzEsIDIsIHszLCA0fSwgWzUsIDZdXX0pCgpmb3IgeCBpbiByZWN1cnNpdmVfaXRlcmF0b3IoZyk6CiAgICBwcmludCh4LCBlbmQ9IiAiKQ==