1

I found a similar question here but the answers don't seem to apply to my issue.

Here is my code:

y = 3
list1 = [1,2,3,4,5]

if y != 0 or y != list1:
    print("y is not in range")
else:
    print(y)

It keeps printing y is not in range.

My goal is to check if y does not equal to 0 or if y does not equal to any item in the list.


I understand that the above or should be an and, I'm specifically interested in how to check in the condition of y being contained in the list.

Bakuriu
  • 98,325
  • 22
  • 197
  • 231
coolpy
  • 163
  • 1
  • 2
  • 14
  • Do you understand looping? Look up `for` loops. And this always prints that line because `y != list1` will always be true. A list will never be equal to a number. – Carcigenicate Jul 25 '16 at 18:53

2 Answers2

10

You want to check that y is different from 0 AND not in the list:

if y != 0 and y not in list1:

Using or means that one of the conditions is sufficient, so since y != 0 it returns True without going to y != list1 which would always return False because an int is not a list, you have to use in in that case.


If you really want to use an or then you what you want:

if not (y == 0 or y in list1):
    print('y not in range')
else:
    print(y)

Rememer the De Morgan laws:

not (y == 0 or y in list1) == (not y == 0) and (not y in list1) == y != 0 and y not in list1
Bakuriu
  • 98,325
  • 22
  • 197
  • 231
  • While this is the right answer for this particular problem, the title of the question suggests a slightly different question than the one in the body. – Carcigenicate Jul 25 '16 at 18:54
  • 2
    @Carcigenicate So edit the title? Titles and questions frequently mismatch and I have never seen any suggestion that we should ignore the question and answer the title instead. – Two-Bit Alchemist Jul 25 '16 at 18:55
  • 1
    @Carcigenicate The title should be a summary of the question. The question *is the body*. If the don't match the title should be edited to match the body. I'll later edit the question to make it clearer and try to find a better title. – Bakuriu Jul 25 '16 at 18:56
  • @Bakuriu, while your answer is right, I believe the portion for `TypeError` may be incorrect for this instance. As it only applies to `<, <=, >, >=` [comparisons](https://docs.python.org/3/library/stdtypes.html#comparisons). – Cory Shay Jul 25 '16 at 19:02
  • @CoryShay Yeah, you are right. I was thinking of `<` and then forgot about it. Fixed. – Bakuriu Jul 25 '16 at 19:03
  • thank you! the `not in` was the keyword/s I was looking for. – coolpy Jul 25 '16 at 19:04
  • @Bakuriu the change in the title does not seem to reflect what my question originally is about and it will make it more difficult for others to find such a question with such an answer in the future. I wanted to ask about: "if conditional statement against each item in a list". the part where I used "or" was just a flaw in my logic, so please change the title to reflect more about "y not in list1" and less about the flawed logic of my 'or'. thanks – coolpy Jul 25 '16 at 19:24
  • @coolpy I don't think so. Your question was. "why does this code always print X" and the answer was that it did that specifically because you used `or`. If you used `and` with the same condition it would always have taken the second branch and never the first one. In any case, since you wanted to ask about `in` your question is a duplicate and I've marked it as such. – Bakuriu Jul 25 '16 at 19:52
1

Regarding the question asked in the title, you can write something like

true if condition else false for y in list1

to check condition for every element y in the list list1. HERE a useful link.

Otherwise @Bakuriu response is correct.

Community
  • 1
  • 1
pez
  • 79
  • 8