Iteration weirdness

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


In article <dave.opstad-DE5822.10313102062004 at reader0903.news.uu.net>,
 Dave Opstad <dave.opstad at agfamonotype.com> wrote:

> 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

Never mind, brain freeze on my part. Should have been:

[d[-1-i] for i in range(len(d))]

Sigh...sorry to bother everyone.

Dave



More information about the Python-list mailing list