Given the following list,
a = [[1,2,3],1,[1,2,43,5],[1,23,4,4],6.5,[1,1,2,45]]
I want to go through all its elements. As you see when the subset has only one element I do not have a list of 1 but just the element alone. So of course this does not work because the second element of a
is not iterable,
for x in a:
for i in x:
print(i)
#do much more
Error:
for i in x:
TypeError: 'int' object is not iterable
I can do the following, but I find it very unhandy, because I have to copy code, or call a function in the part '#do much more
. Any idea?
for x in a:
if type(x) is list:
for i in x:
print(i)
#do much more
else:
print(x)
#do much more (the same as above)