Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #72001 > unrolled thread
| Started by | Igor Korot <ikorot01@gmail.com> |
|---|---|
| First post | 2014-05-25 05:55 -0700 |
| Last post | 2014-05-25 23:58 +1000 |
| Articles | 5 — 5 participants |
Back to article view | Back to comp.lang.python
Loop thru the dictionary with tuples Igor Korot <ikorot01@gmail.com> - 2014-05-25 05:55 -0700
Re: Loop thru the dictionary with tuples Paul Rubin <no.email@nospam.invalid> - 2014-05-25 05:59 -0700
Re: Loop thru the dictionary with tuples Tim Chase <python.list@tim.thechases.com> - 2014-05-25 08:07 -0500
Re: Loop thru the dictionary with tuples Roy Smith <roy@panix.com> - 2014-05-25 09:22 -0400
Re: Loop thru the dictionary with tuples Chris Angelico <rosuav@gmail.com> - 2014-05-25 23:58 +1000
| From | Igor Korot <ikorot01@gmail.com> |
|---|---|
| Date | 2014-05-25 05:55 -0700 |
| Subject | Loop thru the dictionary with tuples |
| Message-ID | <mailman.10291.1401022510.18130.python-list@python.org> |
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.
[toc] | [next] | [standalone]
| From | Paul Rubin <no.email@nospam.invalid> |
|---|---|
| Date | 2014-05-25 05:59 -0700 |
| Message-ID | <7x61kuc8uc.fsf@ruckus.brouhaha.com> |
| In reply to | #72001 |
Igor Korot <ikorot01@gmail.com> writes:
> 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.
[toc] | [prev] | [next] | [standalone]
| From | Tim Chase <python.list@tim.thechases.com> |
|---|---|
| Date | 2014-05-25 08:07 -0500 |
| Message-ID | <mailman.10293.1401023275.18130.python-list@python.org> |
| In reply to | #72003 |
[Multipart message — attachments visible in raw view] — view raw
On 2014-05-25 05:59, Paul Rubin wrote:
> Igor Korot <ikorot01@gmail.com> writes:
> > 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(): ...
You can even use
for ((k1,k2,k3), value) in mydict.iteritems():
...
if you need to unpack the key at the same time.
-tkc
[toc] | [prev] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2014-05-25 09:22 -0400 |
| Message-ID | <roy-F52825.09220325052014@news.panix.com> |
| In reply to | #72001 |
In article <mailman.10291.1401022510.18130.python-list@python.org>,
Igor Korot <ikorot01@gmail.com> wrote:
> 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
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-05-25 23:58 +1000 |
| Message-ID | <mailman.10297.1401026343.18130.python-list@python.org> |
| In reply to | #72005 |
On Sun, May 25, 2014 at 11:22 PM, Roy Smith <roy@panix.com> wrote:
> 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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web