You could use recursion:
def is_present(seq, elem, n=None):
if n is None:
n = len(seq) - 1
if n == -1:
return False
if set(seq[n]) == set(elem):
return True
return is_present(seq, elem, n-1)
if is_present(my_list, combined):
print("Present")
else:
print("Not present")
There is also a problem with your existing code. You shouldn't have an else inside the loop body because if the very first element in my_list is not equal to combined, it will print Not present even though it may be present in the list later. And it will do this for every element that is not equal to it. This can be fixed with a for-else statement:
my_list = [[1, 2], [4, 6], [8, 3]]
combined = [3, 8]
for value in my_list:
print(value)
if set(combined) == set(value):
print("present")
break
else:
print("absent")
Or you can just wrap it in a function:
def is_present():
my_list = [[1, 2], [4, 6], [8, 3]]
combined = [3, 8]
for value in my_list:
print(value)
if set(combined) == set(value):
return True
return False
absenttwice andpresentonce. Is that intentional?for/elseconstruct. You basically need to add abreakin theifand unindent theelse. Then, the answers below still simplify the loop in a nice way.