I have a variable called 'command' and I want to check whether command contains any of a certain list. For instance, if I have a list with ["a", "b", "c"] and I want to detect if command has any of those elements in it. So if command is "abc" it will return true for that if statement and if command is "ABC" it will return false.
Asked
Active
Viewed 64 times
-1
-
1Be careful that you don't run into the [Scunthorp problem](https://en.wikipedia.org/wiki/Scunthorpe_problem). – Mark Ransom Dec 26 '21 at 17:03
2 Answers
3
def command_contains_element(command, element_list):
return any(c in command for c in element_list)

tmarice
- 458
- 4
- 8
-
I don't understand the syntax for any(). What is the "for" and "in" for? – AgentStrawberry Dec 26 '21 at 17:42
-
1Wouldn't it be `element_list` instead of `candidate_list` in the second line? – Syed Shahzer Dec 26 '21 at 18:01
-1
This might do the work:
isTrue = False
for i in ["a","b","c"]: #List you said
if i in command: #command "abc"
isTrue = True
break
print(isTrue)

Soyokaze
- 157
- 8
-
1This is going to print True or False 3 times, definitely not what the problem asked for. – Mark Ransom Dec 26 '21 at 17:05
-