Iteration weirdness

D

Dave Opstad

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
 
D

Dave Opstad

Dave Opstad said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top