Why is 1000000000000000 in range1000000000000001 so fast in Python 3?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Python 3 offers powerful and often surprising efficiencies in its operations. One such operation is evaluating conditions involving the range() function. When we evaluate the expression 1000000000000000 in range(1000000000000001), it returns False almost instantaneously—despite involving very large numbers. Let's dive into the reasons behind the speed and performance of this operation in Python 3.
Range Object Behavior
1. Lazy Evaluation
In Python 3, the range() function returns a range object, which, unlike lists, does not create a list of numbers. Instead, it generates numbers on-the-fly. This lazy evaluation means that memory is used much more efficiently since the entire list of numbers between 0 and 1000000000000000 is never actually created in memory.
2. O(1) Membership Test
Python 3 provides an O(1) complexity for checking membership in a range object. The key advantage stems from the arithmetic nature of range(). The range object knows its start, stop, and step (even though the default step is 1), so it can calculate in constant time whether a particular integer is within this range.
To determine if 1000000000000000 is in the range, Python:
- Calculates whether the number is greater than or equal to the start of the range.
- Checks if it is less than the stop value.
- Ensures it adheres to the stepping value (step = 1 by default).
Because these checks do not involve iterating over the elements, the operation is executed in constant time, O(1).
Examples
Here's a simple comparison demonstrating different scenarios for the in operation:
In both these examples, even though the numbers involved are extraordinarily large, the operations are quick because of the O(1) nature of the lookup.
Key Points Summary
Here's a table summarizing the information provided:
| Feature | Description |
range() efficiency | Uses lazy evaluation. Avoids large memory allocations. |
| Complexity class | Provides O(1) membership tests. |
| Membership operation | Checks boundary conditions efficiently. |
| Step default | Defaults to 1, simplifying arithmetic checks. |
| Memory usage | Does not materialize lists in memory. Highly efficient with large numbers. |
Additional Insights
Implications for Performance
The O(1) membership test has a significant impact on performance, especially when working with large numerical ranges. Developers can utilize the range() function confidently in scenarios where fast membership checking is crucial.
Comparisons with Python 2.x
In Python 2.x, the range() function returns a list, meaning membership checks are not performed in constant time. Python 3's redesign of range() is a stark improvement in terms of both memory and computational efficiency.
Applicability Beyond Membership Checks
The properties of Python's range() can be leveraged in various contexts beyond membership testing, such as slicing operations, iterating with for-loops, and more.
Conclusion
The Python 3 range object showcases the elegance and efficiency of Python when performing operations on potentially vast data sequences. Its constant time complexity for membership checks and its lazy evaluation strategy make it a vital tool for Python programmers dealing with large-scale computations. As seen in 1000000000000000 in range(1000000000000001), Python 3 takes strides in optimizing what could otherwise be computationally expensive operations.
Related reading
- Why is 2 * (i * i) faster than 2 * i * i in Java?
- Why is accessing any single element in an array done in constant time O1 ?
- Why is an even-odd split 'faster' for MergeSort?
- Why is an unreferenced object not finalized immediately after a call to GC.Collect?
- Why is __aexit__ not fully executed when it has await inside?
- Why is __init__ always called after __new__?
- why is async-await much slower than promises when running them together
- Why is builtin sorted slower for a list containing descending numbers if each number appears twice consecutively?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.