Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #63942

Re: dictionary with tuples

Date 2014-01-14 21:21 +0000
From MRAB <python@mrabarnett.plus.com>
Subject Re: dictionary with tuples
References <CA+FnnTyBnns_hkj4ur1y4ySGD_HqtGpc8GNiFhLio31x6eqbKA@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.5479.1389734518.18130.python-list@python.org> (permalink)

Show all headers | View raw


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.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: dictionary with tuples MRAB <python@mrabarnett.plus.com> - 2014-01-14 21:21 +0000

csiph-web