I have a list: list = ['abd','def','gab','dab']
And I want to check if a certain element say x = 'gab'
is equal to an element in the list.
If so, then I want to do something, otherwise I want to pass. And at the end of the for loop
, if x was not equal to any element in list, then I want to run some batch of code.
Here is my code:
for string in range(len(list)):
if list(string) ==x:
do something...
break
else:
pass
The challenge I have is to write the condition to check if x is not equal to any element in list- how can I write this condition?
Intuitively, I would write this code after the for loop- but the issue is that it will then execute regardless of whether x is equal to an element in list or not, and I don't want this, as I only want this next block of code to execute if x was not equal to an element in list! I hope this makes sense.