Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #72005
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!news.linkpendium.com!news.linkpendium.com!panix!roy |
|---|---|
| From | Roy Smith <roy@panix.com> |
| Newsgroups | comp.lang.python |
| Subject | Re: Loop thru the dictionary with tuples |
| Date | Sun, 25 May 2014 09:22:03 -0400 |
| Organization | PANIX Public Access Internet and UNIX, NYC |
| Lines | 37 |
| Message-ID | <roy-F52825.09220325052014@news.panix.com> (permalink) |
| References | <mailman.10291.1401022510.18130.python-list@python.org> |
| NNTP-Posting-Host | localhost |
| X-Trace | reader1.panix.com 1401024123 8652 127.0.0.1 (25 May 2014 13:22:03 GMT) |
| X-Complaints-To | abuse@panix.com |
| NNTP-Posting-Date | Sun, 25 May 2014 13:22:03 +0000 (UTC) |
| User-Agent | MT-NewsWatcher/3.5.3b3 (Intel Mac OS X) |
| Xref | csiph.com comp.lang.python:72005 |
Show key headers only | View raw
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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
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
csiph-web