You can just do -
fruit = {'apple', 'banana', 'orange', 'pear'} input_get_param = 'some_other_fruit' if input_get_param in fruit: chosen = input_get_param print 'pear is in the set' else: chosen = ''
I prefer to search for a set first, since the python set implementation uses a hash table as the underlying data structure. This explains the O (1) membership check, since finding an item in a hash table is an O (1) operation on average. Therefore, the search is pretty cheap.
source share