len(obj) simply calls obj.__len__():

>>> [1, 2, 3, 4].__len__()
4

It is therefore not correct to say that len() is always O(1) -- calling len() on most objects (e.g. lists) is O(1), but an arbitrary object might implement __len__ in an arbitrarily inefficient way.

max(obj) is a different story, because it doesn't call a single magic __max__ method on obj; it instead iterates over it, calling __iter__ and then calling __next__. It does this n times (and also does a comparison each time to track the max item it's seen so far), so it must always be at least O(n). (It can be slower if __next__ or the comparison methods are slow, although that would be very unusual.)

For either of these, we don't count the time it took to build the collection as part of the cost of calling the operation itself -- this is because you might build a list once and then call len() on it many times, and it's useful to know that the len() by itself is very cheap even if building the list was very expensive.

Answer from Samwise on Stack Overflow
Top answer
1 of 3
8

len(obj) simply calls obj.__len__():

>>> [1, 2, 3, 4].__len__()
4

It is therefore not correct to say that len() is always O(1) -- calling len() on most objects (e.g. lists) is O(1), but an arbitrary object might implement __len__ in an arbitrarily inefficient way.

max(obj) is a different story, because it doesn't call a single magic __max__ method on obj; it instead iterates over it, calling __iter__ and then calling __next__. It does this n times (and also does a comparison each time to track the max item it's seen so far), so it must always be at least O(n). (It can be slower if __next__ or the comparison methods are slow, although that would be very unusual.)

For either of these, we don't count the time it took to build the collection as part of the cost of calling the operation itself -- this is because you might build a list once and then call len() on it many times, and it's useful to know that the len() by itself is very cheap even if building the list was very expensive.

2 of 3
6

Let's check it:

import time
from matplotlib import pyplot as plt
import numpy as np


def main():
    a = []
    data = []
    for i in range(10_000):
        a.append(i)
        ts_len = time.time()
        _ = len(a)
        te_len = time.time()

        ts_max = time.time()
        _ = max(a)
        te_max = time.time()

        ts_min = time.time()
        _ = min(a)
        te_min = time.time()

        data.append([i, te_len - ts_len, te_max - ts_max, te_min - ts_min])

    data = np.array(data)
    plt.plot(data[:, 0], data[:, 1], "-r", label="len")
    plt.plot(data[:, 0], data[:, 2], "--g", label="max")
    plt.plot(data[:, 0], data[:, 2], ".b", label="min")
    plt.title("Len/max/min")
    plt.xlabel("Size of the list")
    plt.ylabel("Time elapsed (s)")
    plt.legend()
    plt.show()


if __name__ == '__main__':
    main()

Discussions

Why is the complexity of len(list) O(1)?
len is an O(1) because in your RAM, lists are stored as tables (series of contiguous addresses). To know when the table stops the computer needs two things : length and start point. That is why len() is a O(1), the computer stores the value, so it just needs to look it up. More on reddit.com
🌐 r/learnpython
6
7
October 8, 2017
Time Complexity of Python dictionary len() method - Stack Overflow
Example: a = {a:'1', b:'2'} len(a) What is the time complexity of len(a) ? More on stackoverflow.com
🌐 stackoverflow.com
c - What is the secret behind Python's len() builtin time complexity of O(1) - Stack Overflow
Since Python is implemented in C, I am confused how the developers managed to make the Python builtin len function run on any sequence in constant time, O(1), while C's string function strlen runs in More on stackoverflow.com
🌐 stackoverflow.com
September 2, 2018
Why len(list) has a time-complexity of O(1)?
I mean the implementation keeps a variable that holds the current length of the list. So a call to len would just return the current value of the variable, making it O(1). When the list is changed, elements added or removed, the variable holding the current length is also updated accordingly. More on reddit.com
🌐 r/leetcode
38
41
July 13, 2023
🌐
Python
wiki.python.org › moin › TimeComplexity
TimeComplexity - Python Wiki
The second one is O(len(t)) (for every element in t remove it from s). So care must be taken as to which is preferred, depending on which one is the longest set and whether a new set is needed. To perform set operations like s-t, both s and t need to be sets. However you can do the method equivalents even if t is any iterable, for example s.difference(l), where l is a list. The Average Case times listed for dict objects assume that the hash function for the objects is sufficiently robust to make collisions uncommon.
🌐
GeeksforGeeks
geeksforgeeks.org › python › complexity-of-len-with-regard-to-sets-and-lists
Complexity Of Len() With Regard To Sets And Lists - GeeksforGeeks
July 23, 2025 - However, time complexity can vary for other operations on lists and sets. For example, iterating over the elements of a list or set would have a linear time complexity, O(n), where n is the number of elements in the collection.
🌐
Medium
medium.com › @hamzaehsankhan › why-len-list-has-a-time-complexity-of-o-1-a-dive-into-cpython-cbed75c38b54
Why len(list) has a time-complexity of O(1)? — A dive into CPython | by Hamza Ehsan Khan | Medium
July 13, 2023 - This in-built function is not at the mercy of the size of the list. Whether your list contains 1 element or 1000, as per the default implementation of Python (CPython), the time-complexity is O(1).
🌐
Reddit
reddit.com › r/learnpython › why is the complexity of len(list) o(1)?
r/learnpython on Reddit: Why is the complexity of len(list) O(1)?
October 8, 2017 -

I was looking at the time complexity of different list operations and the "get length" operation has constant time O(1). Why is that? I would understand it if lists could only store a single type of object (e.g. only integers or characters), but since lists can hold objects of a different size I don't understand how the len() function has O(1) complexity.

🌐
Finxter
blog.finxter.com › home › learn python blog › python list length – what’s the runtime complexity of len()?
Python List Length - What's the Runtime Complexity of len()? - Be on the Right Side of Change
April 8, 2020 - The runtime complexity of the len() function on your Python list is O(1). It takes constant runtime no matter how many elements are in the list. Why? Because the list object maintains an integer counter that increases and decreases as you add and remove list elements. Looking up the value of this counter takes constant time...
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › internal-working-of-the-len-function-in-python
Internal Working of the len() Function in Python - GeeksforGeeks
July 2, 2020 - Thus when you call the len() function, you do not give the interpreter the command to find the length by traversing, but rather you ask the interpreter to print a value that is already stored. Hence, len() function in Python runs in O(1) complexity.
🌐
Copahost
copahost.com › home › len python: complete guide to python’s len() function
Len Python: complete guide to Python's len() function - Copahost
August 5, 2023 - While most Python implementations of the len function have O(1) time complexity for fixed-size objects, some implementations may have O(n) time complexity for variable-size objects such as lists or dictionaries.
🌐
Finxter
blog.finxter.com › python-len
Python len() – Be on the Right Side of Change
The runtime complexity of the len() function on your Python list is O(1). It takes constant runtime no matter how many elements are in the list. Why? Because the list object maintains an integer counter that increases and decreases as you add and remove list elements. Looking up the value of this counter takes constant time...
🌐
Scaler
scaler.com › home › topics › python length of list
Python Length of List - Scaler Topics
May 4, 2023 - The length_hint() method in python works the same way as len() method, it applies to more data types than len() method. Also, it does not always return the exact number of elements in the list. Naive method uses a loop and a counter. It visits each element in the list and increments the counter. It is easy to implement and doesn't require pre-defined functions. Time complexity of the Naive method is O(n) whereas that of len() and length_hint() is O(1).
🌐
Qissba
qissba.com › home › blog › length function in python with examples | cbse – class 12
Length Function in Python with Examples | CBSE - Class 12 Qissba -
April 28, 2023 - Ans. The time complexity of the len() function is O(1) as it simply returns the length of a sequence without iterating over the elements.
🌐
AlgoCademy
algocademy.com › link
Time Complexity Practice 1 in Python | AlgoCademy
We will break down the time complexity of each built-in function step-by-step: len(): O(1) - The length of a list, string, or dictionary is stored as an attribute, so accessing it is a constant time operation.
🌐
UCI
ics.uci.edu › ~pattis › ICS-33 › lectures › complexitypython.txt
Complexity of Python Operations
Here we copy the list so as to ... class of this function's body. def is_unique2 (alist : [int]) -> bool: copy = list(alist) O(N) copy.sort() O(N Log N) - for fast Python sorting for i in range(len(alist)-1): O(N) - really N-1, but that is O(N); len and - ar...
🌐
HatchJS
hatchjs.com › home › python len() function time complexity: a guide for developers
Python len() Function Time Complexity: A Guide for Developers
December 26, 2023 - The `__len__()()` method should return the number of elements in the object. For example, the following code will print the number of elements in the list [1, 2, 3, 4, 5]: ... the Python len() function has a time complexity of O(1).
🌐
Reddit
reddit.com › r/leetcode › why len(list) has a time-complexity of o(1)?
r/leetcode on Reddit: Why len(list) has a time-complexity of O(1)?
July 13, 2023 - Its a variable length vector, where the length is extended whenever the number of items that we want to store is larger than the allocated space. It doesn't make any sense for python lists to be implemented as linked lists, as the random access speed would be horrible and unusable for certain scenarios.
🌐
Unstop
unstop.com › home › blog › find length of list in python | 8 methods + code examples
Find Length Of List In Python | 8 Methods + Code Examples
February 3, 2025 - While this method is clear and works for small lists, it is inefficient for large datasets due to its O(n) time complexity. For large lists, you may want to use more optimized methods like len(), which we will discuss in the next section. The len() function is the go-to method for finding the length of a list in Python.