2 ms·
In python 3 range() does not build a list but produce a range object. Testing x in range(a,b) will actually call the __contains__ method of the object which is
by rnhmjoj 11y ago
In python 3 range() does not build a list but produce a range object. Testing x in range(a,b) will actually call the __contains__ method of the object which is O(1).
The only overhead here may be the object creation.
See this http://stackoverflow.com/q/30081275 http://stackoverflow.com/q/30081275
- daveguy 11y agoIt may be O(1), but the constant of a __contains__ method is almost certainly much higher than the constant of an if 1 <= len(vals) <= 5: statement.