Python program to check if a specified element presents in a tuple of tuples

Viewed 54

I have a tuple which contains integers,strings and tuples(nested tuple). I want to check whether an element present in the tuple( in nested tuple also). this is what i have tried so far

tu=(1,'priya',2,(1,2,3,4),5,6,(7,'hi'),8,9)
if any('hi' in i for i in c for c in tu):
    print("exists")
else:
    print("does not exist")

it shows an error like this

if any('hi' in i for i in c for c in tu): NameError: name 'c' is not defined

kindly help

1 Answers
searched = 'hi'
for i in tu:
    if isinstance(i, tuple):
        if searched in i:
            print('exists')
            break
    elif searched == i:
        print('exists')
        break
else:
    print('does not exist')

this should work

Related