What is the better way of doing this:
if pattern[0] != [0] or pattern[0] != [0 , 0] or ... so on:
# do something
1 Answer 1
It seems like you are looking for any:
if any(pattern[0]):
This solution tests if any of the items in pattern[0] are not equal to 0. It works because 0 evaluates to False in Python. Of course, it also assumes that pattern[0] is iterable since you were comparing it to lists originally.
Also, the condition of your if-statement is incorrect regardless of what you are trying to do. It will always be True because pattern[0] will always be either not equal to [0] or not equal to [0, 0]. You should be using and instead of or:
if pattern[0] != [0] and pattern[0] != [0 , 0] and ... so on:
Sign up to request clarification or add additional context in comments.
2 Comments
paxdiablo
Until OP clarifies the question re use of
and or or, this is technically wrong. It's probably right but you may want to note the discepency in your answer.b8ckwith
@paxdiablo well I need it for OR(as i asked above), but will be nice to have with AND for future reference.
lang-py
pattern[0]can't be both[0]and[0,0]at the same time, you can change the whole thing toif True::-) Or did you meanandrather thanor?elsecase?patternis[[0, 0]]could it not fitpattern[0] != [0 , 0]?[[0,0]], thenpattern[0] != [0]is true. In fact, the wholeifstatement condition is true regardless of whatpatternis set to. That's my point.