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]]})
I am trying to iterate recursively over g
.
How to iterate each and every element recursively in python which works for any list with any level of nesting ?
I have tried with hasattr
, __iter__
but won't work with unknown level of nesting.
f=(3,4,5,{3:4},[6,7,8])
g = (1, 2, [3, 4, [5, 6], {7: 8}], 9, 10, {11: (3, 4, 5, {3: 4}, [16, 7, 8])}, {12: [1, 2, set([3, 4]), [5, 6]]})
print g
for each in g:
print each
try:
if hasattr(each,"__iter__"):
for ind in each:
print ind
if hasattr(ind,"__iter__"):
for ind1 in ind:
print ind1