[Tutor] Help with a Dictionary

spir denis.spir at free.fr
Fri Jan 8 12:04:45 CET 2010


Garry Bettle dixit:

> for fixture in FixtureList:
>     print fixture.ljust(6), FixtureList[fixture]
...
> for fixture, racetimes in FixtureList:
>     print fixture, racetimes
> 
> Traceback (most recent call last):
>   File "<pyshell#3>", line 1, in <module>
>     for fixture, racetimes in FixtureList:
> ValueError: too many values to unpack

Because iterating on a dict actually iterates on its keys -- the way you it in previous axample. To get directly (key, value) tuples instead, use dict.items() instead:
for (fixture, racetimes) in FixtureList.items():
    print fixture, racetimes

(parens are optional, I just use them for clarity)

Note: FixtureTimes may be a better name for your dict (usually a dict name in the form of key_values does the job well, except when key is obvious/implicit).

Denis
________________________________

la vita e estrany

http://spir.wikidot.com/


More information about the Tutor mailing list