Whenever I needed to check whether an item belongs to a given group I used to store the elements in a list and use in
to make the check:
def is_in_list(element, l):
if element in l:
print('yes')
else:
print('no')
is_in_list(2, [1,2,3])
This solution does not require for the elements to be sorted, or for the list to even be made exclusively of numbers. I guess that in the specific case I already know that the elements are all integer numbers and that they are sorted there must be a more efficient way to do this.
What is the best way to organize my data so that it will be quick to verify whether a given number belongs to a list?
(Ps. I call it "list", but it is not important for it to be a list specifically, and if it is recommended, I can very well use arrays or dictionaries instead).