Question

What is the cost of len() function for Python built-ins? (list/tuple/string/dictionary)

1
248
8/13/2014 9:39:23 PM

Accepted Answer

It's O(1) (constant time, not depending of actual length of the element - very fast) on every type you've mentioned, plus set and others such as array.array.

287
2/27/2017 3:22:24 PM

Calling len() on those data types is O(1) in CPython, the most common implementation of the Python language. Here's a link to a table that provides the algorithmic complexity of many different functions in CPython:

TimeComplexity Python Wiki Page


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon