Cost of len() function

Imran picture Imran · Jul 12, 2009 · Viewed 87.7k times · Source

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

Answer

Alex Martelli picture Alex Martelli · Jul 12, 2009

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.