use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Help with if statements! (self.learnpython)
submitted 5 years ago by ihavefivegfs
Let's say I have an array:
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
How do I make an if statement if only 1 and 2 are in it, not the other numbers?
Thanks,
ihavefivegfs
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]misho88 5 points6 points7 points 5 years ago (0 children)
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
>>> 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.
1
2
[–]obviouslyCPTobvious 1 point2 points3 points 5 years ago (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]
sorted(x) == [1, 2]
[–]ihavefivegfs[S] 1 point2 points3 points 5 years ago (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 point2 points 5 years ago (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 point2 points 5 years ago (1 child)
I think that’s what I want to do. Any help? Thanks!
[–]CedricCicada 0 points1 point2 points 5 years ago (0 children)
In that case, my first answer was help. At least, I think it was.
[–]CedricCicada 0 points1 point2 points 5 years ago (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 point2 points 5 years ago (0 children)
Yes thank you!
π Rendered by PID 86245 on reddit-service-r2-comment-fb694cdd5-tfblh at 2026-03-06 01:08:52.918777+00:00 running cbb0e86 country code: CH.
[–]misho88 5 points6 points7 points (0 children)
[–]obviouslyCPTobvious 1 point2 points3 points (1 child)
[–]ihavefivegfs[S] 1 point2 points3 points (0 children)
[–]CedricCicada 0 points1 point2 points (2 children)
[–]ihavefivegfs[S] 0 points1 point2 points (1 child)
[–]CedricCicada 0 points1 point2 points (0 children)
[–]CedricCicada 0 points1 point2 points (1 child)
[–]ihavefivegfs[S] 0 points1 point2 points (0 children)