dictionary with tuples

MRAB python at mrabarnett.plus.com
Tue Jan 14 16:21:56 EST 2014


On 2014-01-14 21:10, Igor Korot wrote:
> Hi, ALL,
> C:\Documents and Settings\Igor.FORDANWORK\Desktop\winpdb>python
> Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit
> (Intel)] on win32
> Type "help", "copyright", "credits" or "license" for more information.
>>>> dict = {}
>>>> dict[(1,2)] = ('a','b')
>>>> dict[(3,4)] = ('c','d')
>>>> for (key1,key2),(value1,value2) in dict:
> ...     print key1, " ", key2
> ...     print value1, " ", value2
> ...
> Traceback (most recent call last):
>    File "<stdin>", line 1, in <module>
> TypeError: 'int' object is not iterable
>>>>
>
> What am I doing wrong?
>
> Thank you.
>
When you iterate over a dict it yields the only the keys, not the keys
and values. Try iterating over dict.items() instead.

By the way, try not to use names such as 'dict' that are the names of
built-in classes.



More information about the Python-list mailing list