I'm new to python and trying to complete an exercise where I print every variable from a list including any nested lists.
My issue is I can't get the nested lists to be recognised as lists by an if statement.
When I run type(i) it returns that it is a list however when I run if type(i) is list
or if type(i) == list
it fails to execute.
When I tried using if isinstance(type(i), list)
I get a TypeError:isinstance() arg 2 must be a class, type, or tuple of classes and types.
When I tried isinstance(type(i),collections.Sequence)
the nested list isn't recognised as a list either.
If anyone has any advice it'd be appreciated. I'm using Python 2.6 as I'm following the MIT course.
Thanks
# -*- coding: cp1252 -*-
import collections
listval= ["war",1,["brie","rocky","roq le coq"],[1,2,3]]
def printlist2(lists):
for i in lists:
print("Variable value: ", type(i))
print ("Is variable a list: ",isinstance(type(i),collections.Sequence))
#print (isinstance(type(i),list))
if isinstance(type(i),collections.Sequence):
print ("This is a list")
printlist2(i)
elif type(i) == list:
print ("This is a list")
elif type(i) is int:
#print ("String length is equal to ",len(str(i)))
print ("i is equal to integer ",i)
else:
#print ("String length is equal to ",len(i))
print ("i is equal to string ",i)
printlist2(listval)