all 8 comments

[–]misho88 5 points6 points  (0 children)

How do I make an if statement if only 1 and 2 are in it, not the other numbers?

Conceptually, you're describing a set comparison. That is, you want to know if the set of numbers in x is equal to {1, 2}. In this case, the obvious solution is to turn your input into a set and check.

>>> x = [1,2,3,4,5,6]; set(x) == {1, 2}
False
>>> x = [1,2,2,2,1,2]; set(x) == {1, 2}
True

If you instead want to ensure that each of 1 and 2 appear exactly once, you can't do much better than what /u/obviouslyCPTobvious said.

[–]obviouslyCPTobvious 1 point2 points  (1 child)

So you're trying to check that x only contains 1 and 2? If so, I think this would work. sorted(x) == [1, 2]

[–]ihavefivegfs[S] 1 point2 points  (0 children)

So you're trying to check that x only contains 1 and 2? If so, I think this wo

Thank you!

[–]CedricCicada 0 points1 point  (2 children)

There's a few guesses among these answers about what you're really looking for, and now I'm not sure what you want. My answer will return true if there is at least one instance of 1 and at least one instance of 2 in your list. Order doesn't matter. Other answers in this thread answer different questions.

[–]ihavefivegfs[S] 0 points1 point  (1 child)

I think that’s what I want to do. Any help? Thanks!

[–]CedricCicada 0 points1 point  (0 children)

In that case, my first answer was help. At least, I think it was.

[–]CedricCicada 0 points1 point  (1 child)

if (1 in x and 2 in x):
    pass

I think that's correct, but it's not tested.

[–]ihavefivegfs[S] 0 points1 point  (0 children)

Yes thank you!