In Python one can very easily check if a value is contained in a container by using the in
-operator. I was wondering why anyone would ever use the in
-operator on a list, though, when it's much more efficient to first transform the list to a set as such:
if x in [1,2,3]:
as opposed to
if x in set([1,2,3]):
When looking at the time complexity, the first one has O(n) while the second one is superior at O(1). Is the only reason to use the first one the fact that it's more readable and shorter to write? Or is there a special case in which it's more practical to use? Why did the Python devs not implement the first one by first translating it to the second one? Would this not grand both of them the O(1) complexity?