Dictionary is really not easy to handle

jfong at ms4.hinet.net jfong at ms4.hinet.net
Thu Apr 28 04:27:06 EDT 2016


I have a dictionary like this:

>>> dct ={1: 'D', 5: 'A', 2: 'B', 3: 'B', 4: 'E'}

The following code works:

>>> for k in dct: print(k, dct[k])
...
1 D
2 B
3 B
4 E
5 A

and this one too:

>>> for k,v in dct.items(): print(k,v)
...
1 D
2 B
3 B
4 E
5 A

But...this one?

>>> for k,v in dct: print(k,v)
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable

No idea what the error message means:-( Can anyone explain it? Thanks ahead.



More information about the Python-list mailing list