Loop thru the dictionary with tuples

I

Igor Korot

Hi, ALL,
I have a following data structure:

my_dict[(var1,var2,var3)] = None
my_dict[(var4,var5,var6)] = 'abc'

What I'm trying to do is this:

for (key,value) in my_dict:
#Do some stuff

but I'm getting an error "Too many values to unpack".

What am I doing wrong?

Thank you.
 
P

Paul Rubin

Igor Korot said:
for (key,value) in my_dict:
#Do some stuff

but I'm getting an error "Too many values to unpack".

Use
for (key,value) in mydict.iteritems(): ...
otherwise you loop through just the keys, whicn in your dictionary
happens to be 3-tuples. So you try to unpack a 3-tuple to a 2-tuple
and get a too-many-values error.
 
T

Tim Chase

Use
for (key,value) in mydict.iteritems(): ...

You can even use

for ((k1,k2,k3), value) in mydict.iteritems():
...

if you need to unpack the key at the same time.

-tkc




-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)

iQEcBAEBCgAGBQJTgesCAAoJEN385Nh6n9aQk4UH/3+OF3sZ9wa4WFEnQpnyUaEv
vEebZwWSdvHreG+WYjuT3modBuXSLTLWlSJhYCaHYWZn0boYbcoOKuz+D4XCwJYD
RLbuehlHCyumwfnwSsIpL0C2YxbUsV8z5E7ffIFaE2KSli9F52CXN2F0e6O4eCY1
0iBvNhxZU3hVCMRDJws/nXB1KCSTmWAYXNGQYTq8vYcYolLTM3EdjXOD02RsXLTr
9eru+QanL6Ccddzf9ehaMuP0ke/hY9sw36uRpNr13QxDMLC9P9aXi3ymz2eFO8Tl
vvqfLMTtVlHr5QQO2dYuXW9DhwvxGnpue4PVo7Nixizj4EgYSA1/wtvR9IW1jUA=
=4/Po
-----END PGP SIGNATURE-----
 
R

Roy Smith

Igor Korot said:
for (key,value) in my_dict:
#Do some stuff

but I'm getting an error "Too many values to unpack".

Several people have already given you the right answer, so I'll just
suggest a general debugging technique. Break this down into the
smallest possible steps and print out the intermediate values. When you
write:
for (key,value) in my_dict:

two things are happening. One is that you're iterating over my_dict,
the other is that you're unpacking the iterated-over things. So break
those up into individual steps:

for thing in my_dict:
(key, value) = thing

and see what that gives you. Do you still get an error? If so, does it
occur on the "for" line or on the assignment line? Hint: in this case,
it will happen on the assignment line, so, your next step is to print
everything out and see what's going on:

for thing in my_dict:
print thing
(key, value) = thing

At this point, it should be obvious what's going on, but just in case
it's not, sometimes I find it useful to be even more verbose:

for thing in my_dict:
print type(thing), repr(thing)
(key, value) = thing
 
C

Chris Angelico

Hint: in this case,
it will happen on the assignment line, so, your next step is to print
everything out and see what's going on:

for thing in my_dict:
print thing
(key, value) = thing

Aside: I know that you (Roy) are still using Python 2, but the OP
could be on either branch. As a matter of safety, I'd put parens
around the print:

for thing in my_dict:
print(thing)
(key, value) = thing

That way, it works on either.

ChrisA
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top