I would like to loop the nest list ['sally','joe'] in the example shown below.
data = ['joe','mike',['sally','joe'],'phil']
I attempted the following:
for i in data:
for j in (i):
if type(j) == '<class '+"'list'>":
print(j)
Why not just isinstance:
for i in data:
if isinstance(i,list):
print(i)
Now the output is:
['sally', 'joe']
You would need to use:
if type(j) == list:
print(j)
It doesn't currently work because type(j) returns an object of class type, not a string. You might think it is a string because when printing it in a REPL interpreter, you might see the repr(..) version.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With