Iteration over Lists and Strings

Andrew Durdin adurdin at gmail.com
Sun Aug 29 00:34:11 CEST 2004


On Sat, 28 Aug 2004 19:09:53 GMT, Brent W. Hughes
<brent.hughes at comcast.net> wrote:
> 
> Did you say enumerate(seq) takes constant time?  I would have thought it was
> proportional to len(seq).

enumerate(seq) is O(1)
index(seq) is O(N)

And thus:

for i, val in enumerate(seq):  print i   is O(N)
for val in seq:  print seq.index(val)   is O(N*N)



More information about the Python-list mailing list