[Tutor] for k,v in d: ValueError: too many values to unpack

John Fouhy john at fouhy.net
Tue May 22 00:50:14 CEST 2007


On 22/05/07, John Washakie <washakie at gmail.com> wrote:
> I have a Dictionary, that is made up of keys which are email
> addresses, and values which are a list of firstname, lastnamet,
> address, etc...
>
> If I run the following:
>
> last = {}
[...]
> for k,v in last:
>         print "Email: %s , has the Last name: %s" % (k,v[0])
>
> I get the error indicated in the subject:
> ValueError: too many values to unpack

The "implicit" iteration that dictionaries support only iterates over keys.

i.e. you could have done this:

for k in last:
    print "Key is %s, value is %s" % (k, last[k])

Alternatively, you can use the iteritems() method;

for k, v in last.iteritems():
    print "Key is %s, value is %s" % (k, v)

See http://www.python.org/doc/current/lib/typesmapping.html for details.

-- 
John.


More information about the Tutor mailing list