Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

dictionary with tuples

38 views
Skip to first unread message

Igor Korot

unread,
Jan 14, 2014, 4:10:07 PM1/14/14
to pytho...@python.org
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.

Larry Martell

unread,
Jan 14, 2014, 4:15:43 PM1/14/14
to Igor Korot, pytho...@python.org
You need to iterate over dict.items()

Tim Chase

unread,
Jan 14, 2014, 4:18:57 PM1/14/14
to pytho...@python.org
On 2014-01-14 13: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:
>
> What am I doing wrong?

You should iterate over either dict.items() or dict.iteritems()

Also, it's bad practice to shadow the builtin "dict()", so I'd choose
another name:

d = {}
d[(1, 2)] = ('a', 'b')
d[(3, 4)] = ('c', 'd')
for (k1, k2), (v1, v2) in d.iteritems():
do_something(k1, k2, v1, v2)

-tkc


YBM

unread,
Jan 14, 2014, 4:21:15 PM1/14/14
to
Le 14/01/2014 22:10, Igor Korot a �crit :
for ... in dict:

is a way to iterate through dictionnary items,

what you want to do can be done so:

for (key1,key2),(value1,value2) in dict.items():



MRAB

unread,
Jan 14, 2014, 4:21:56 PM1/14/14
to pytho...@python.org
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.

Dave Angel

unread,
Jan 14, 2014, 4:32:39 PM1/14/14
to pytho...@python.org
Igor Korot <ikor...@gmail.com> Wrote in message:
> 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.
>

Two things. You really shouldn't shadow a builtin with your own
locals, though it hasn't hurt you this time. Next time pick a
different name than dict.

Your real problem is that you're trying to iterate over items,
but forgot to use that method. The for loop line should
end

in dict.items () :


You may be confused, since it's different in python 3.x

--
DaveA



----Android NewsGroup Reader----
http://www.piaohong.tk/newsgroup

Tobiah

unread,
Jan 14, 2014, 5:00:05 PM1/14/14
to
But it's (key1, value1), (key2, value2)

emile

unread,
Jan 14, 2014, 5:14:08 PM1/14/14
to pytho...@python.org
No it isn't. :)

Emile


YBM

unread,
Jan 14, 2014, 9:24:21 PM1/14/14
to
Le 14/01/2014 23:00, Tobiah a �crit :
No. Try it.

key1 key2 are the members of every tuple key.
value1, value2 are the members of every tuple value.

>>> d={ (1,2):('a','b'), (3,4):('c','d') }
>>> for (key1,key2),(value1,value2) in d.items():
... print key1,key2,value1,value2
...
1 2 a b
3 4 c d



0 new messages