Iteration weirdness

Dave Opstad dave.opstad at agfamonotype.com
Wed Jun 2 13:31:33 EDT 2004


I'm running into a strange behavior under Python 2.3.3:

------------------------------------------
>>> d = {-1: 'cv', -2: 'se', -3: 'se'}
>>> d
{-1: 'cv', -2: 'se', -3: 'se'}
>>> len(d)
3
>>> [d[-1], d[-2], d[-3]]
['cv', 'se', 'se']
>>> [d[-1-i] for i in len(d)]
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: iteration over non-sequence
------------------------------------------

Can someone enlighten me as to why the list comprehension gives an 
error, but the simple list construction case works fine?

Thanks for any help!
Dave Opstad



More information about the Python-list mailing list