Algorithmic complexity of len (list)?

Tim Peters tim.peters at gmail.com
Sat Jul 3 01:47:47 CEST 2004


[Roy Smith]
> Is the length of a list stored in the object, or does len() have to
> count the elements each time you call it?  In other words, is len (list)
> O(1) or O(n)?

O(1).  Ditto len(tuple), len(dict), len(str), len(unicode), and
len(array.array).




More information about the Python-list mailing list