s = set([1,2,3])
I could do this:
1 in s
#=> True
I’m wondering without using loop, is there a way to do something like:
1,2 in s
#=>True
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can use set intersection:
Of course this still has a loop internally.
As spicavigo pointed out you can use
issuperset, or more concisely you can use>=which is equivalent:Again it still has a loop internally, but you probably can’t do it any better than that.
See the documentation for more information about set operations.
You can further improve performance if you create your set
{1, 2}only once one your program starts and store it, rather than recreating it every time you make the test.