Write a python program using recursion to traverse every single element of the nested list. [10,20,15,[11,”hello”,25.5,[123,15,”hi”,[10,20],52],100,200],15,50,[51,”welcome”],1000 ]
Answers
Answered by
0
Explanation:
My suggestion would've been to do something like this:
def traverse(o, tree_types=(list, tuple)):
if isinstance(o, tree_types):
for value in o:
for subvalue in traverse(value, tree_types):
yield subvalue
else:
yield o
#give a list of all values [2, 4, 6, 8, 10, 12, 14, 16]
def inthere(ls,s):
if s in list(traverse(ls)): #check if your value is in that list
return True
else:
return False
A=[[2,4],[6,[[[8],10]],12],14,16]
print(inthere(A,12)) #return True
print(inthere(A,2)) #Returns True
Hope it helps =)
Similar questions