[Tutor] Iterate Through a Dictionary where the Values are Tuples

Alan Gauld learn2program at gmail.com
Thu Jan 27 19:10:11 EST 2022


On 27/01/2022 23:08, Christopher Gass wrote:
> what's being requested in the response plan. When I try to
> iterate through "details" however, I'm receiving a "too many values to
> unpack" error. 

Please, always include the full error traceback. It contains a lot of
detail that can help diagnose the problem.

Especially in a very complex bit of code like this where multiple
aliases and several interlinked data
structures are being used.  (BTW Have you considered converting this to
a database? It looks to me
like SQL might be a better fit! SQLite is ideal for this kind of project.)

Another option might be an OOP approach, most of those dictionaries
represent objects and you could
move a lot of the code into methods, thus avoiding the explicitly heavy
nesting of loops. (They will
still be nested, but hidden inside method calls) It should be more
readable if nothing else!

> units = {
>     'A2':('Aid Unit', 'STA 2'),
>     'E2':('Engine', 'STA 2'),
> }
>
> def recommendations(call_type,grid):
>     call_type = call_type.strip().upper()
>     grid = grid.strip().upper()
>     result = []
>     apparatus = []
>     i = 0
>     unit_details = ()
>     radio_position = get_radio(grid)
>     if radio_position == 'TAC_1':
>         radio_position = TAC_1
>     response_plan = radio_position[call_type]
>     rec_station_order = station_order[grid]
>     for unit_type in response_plan:
>         for x in unit_type:
>             for station in rec_station_order:

I assume this next bit is where the error occurs?

>                 for unit, details in units.items():
>                     for u_type, unit_station in details:

And the above line specifically? It looks like it should work.

That's why I'd like to see what exactly python is complaining about in
the error message.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list