dictionary with tuples

I

Igor Korot

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):

What am I doing wrong?

Thank you.
 
Y

YBM

Le 14/01/2014 22:10, Igor Korot a écrit :
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):

What am I doing wrong?

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():
 
T

Tobiah

Le 14/01/2014 22:10, Igor Korot a écrit :
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):

What am I doing wrong?

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():

But it's (key1, value1), (key2, value2)
 
E

emile

Le 14/01/2014 22:10, Igor Korot a écrit :
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?

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():

But it's (key1, value1), (key2, value2)


No it isn't. :)

Emile
 
Y

YBM

Le 14/01/2014 23:00, Tobiah a écrit :
Le 14/01/2014 22:10, Igor Korot a écrit :
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?

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():

But it's (key1, value1), (key2, value2)

No. Try it.

key1 key2 are the members of every tuple key.
value1, value2 are the members of every tuple value.
... print key1,key2,value1,value2
...
1 2 a b
3 4 c d
 

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
473,968
Messages
2,570,150
Members
46,697
Latest member
AugustNabo

Latest Threads

Top