Check the variable if it is in the list

I'm new to Python, and I was wondering if there was a concise way of checking the value to see if it is one of the values ​​in the list, similar to the SQL WHERE clause. Sorry if this is the main question.

MsUpdate.UpdateClassificationTitle in ( 'Critical Updates', 'Feature Packs', 'Security Updates', 'Tools', 'Update Rollups', 'Updates', ) 

ie, I want to write:

 if MsUpdate.UpdateClassificationTitle in ( 'Critical Updates', 'Feature Packs', 'Security Updates', 'Tools', 'Update Rollups', 'Updates' ): then_do_something() 
+6
source share
3 answers

It seems concise enough, but if you use it more than once, you should call the tuple:

 titles = ('Critical Updates', 'Feature Packs', 'Security Updates', 'Tools', 'Update Rollups', 'Updates') if MsUpdate.UpdateClassificationTitle in titles: do_something_with_update(MsUpdate) 

Tuples use brackets. If you want the list to change it to square brackets. Or use a set that searches faster.

+12
source

It is pretty simple:

 sample = ['one', 'two', 'three', 'four'] if 'four' in sample: print True 
+11
source

Make sure you use, for example, not

 username = ["Bob", "Kyle"] name = "Kyle" if name in username: print("step 1") login = 1 else: print("Invalid User") 
+2
source

Source: https://habr.com/ru/post/898973/


All Articles