Why is "1000000000000000 in range(1000000000000001)" so fast in Python 3?
It is my understanding that the range() function, which is actually an object type in Python 3, generates its contents on the fly, similar to a generator.This being the case, I would have expected the...
View ArticleAnswer by Martijn Pieters for Why is "1000000000000000 in...
The Python 3 range() object doesn't produce numbers immediately; it is a smart sequence object that produces numbers on demand. All it contains is your start, stop and step values, then as you iterate...
View ArticleAnswer by poke for Why is "1000000000000000 in range(1000000000000001)" so...
To add to Martijn’s answer, this is the relevant part of the source (in C, as the range object is written in native code):static intrange_contains(rangeobject *r, PyObject *ob){ if...
View ArticleAnswer by wim for Why is "1000000000000000 in range(1000000000000001)" so...
Use the source, Luke!In CPython, range(...).__contains__ (a method wrapper) will eventually delegate to a simple calculation which checks if the value can possibly be in the range. The reason for the...
View ArticleAnswer by abarnert for Why is "1000000000000000 in range(1000000000000001)"...
The fundamental misunderstanding here is in thinking that range is a generator. It's not. In fact, it's not any kind of iterator.You can tell this pretty easily:>>> a = range(5)>>>...
View ArticleAnswer by Stefan Pochmann for Why is "1000000000000000 in...
The other answers explained it well already, but I'd like to offer another experiment illustrating the nature of range objects:>>> r = range(5)>>> for i in r: print(i, 2 in r,...
View ArticleAnswer by abarnert for Why is "1000000000000000 in range(1000000000000001)"...
If you're wondering why this optimization was added to range.__contains__, and why it wasn't added to xrange.__contains__ in 2.7:First, as Ashwini Chaudhary discovered, issue 1766304 was opened...
View ArticleAnswer by Sławomir Lenart for Why is "1000000000000000 in...
It's all about a lazy approach to the evaluation and some extra optimization of range.Values in ranges don't need to be computed until real use, or even further due to extra optimization.By the way,...
View ArticleAnswer by RBF06 for Why is "1000000000000000 in range(1000000000000001)" so...
TL;DRThe object returned by range() is actually a range object. This object implements the iterator interface so you can iterate over its values sequentially, just like a generator, list, or tuple.But...
View ArticleAnswer by Naruto for Why is "1000000000000000 in range(1000000000000001)" so...
Due to optimization, it is very easy to compare given integers just with min and max range.The reason that the range() function is so fast in Python3 is that here we use mathematical reasoning for the...
View ArticleAnswer by benjimin for Why is "1000000000000000 in range(1000000000000001)"...
Try x-1 in (i for i in range(x)) for large x values, which uses a generator comprehension to avoid invoking the range.__contains__ optimisation.
View ArticleAnswer by Matej Novosad for Why is "1000000000000000 in...
TLDR;the range is an arithmetic series so it can very easily calculate whether the object is there. It could even get the index of it if it were list like really quickly.
View ArticleAnswer by Mahmoud Salhab for Why is "1000000000000000 in...
__contains__ method compares directly with the start and end of the range
View Article