[Tutor] Multiple Dimension (array/list/whatever it is are).

Alan Gauld alan.gauld at blueyonder.co.uk
Mon May 10 18:40:35 EDT 2004

> 1. What is the data structure below called? 

> G = {'s':{'u':10, 'x':1},
>      'u':{'v':1, 'x':2},
>      'v':{'y':4},
>      'x':{'u':3, 'v':9, 'y':2},
>      'y':{'s':7, 'v':6}
>     }

A dictionary of dictionaries.
( key1:value1, key2:value2,...} 
is how to declare a dictionary, and in this case each value 
is also a dictionary.

> 2. How would I alter the above data structure such that all 
> instances of 'x' ends up being equal to 5. As per sample below.
> {'s':{'u':10, 'x':5},
>      'u':{'v':1, 'x':5},
>      'v':{'y':4},
>      'x':{'u':3, 'v':9, 'y':2},
>      'y':{'s':7, 'v':6}
> }

I assume you mean during program execution?

for key in G:
    try: key['x'] = 5
    except keyError: pass

Or something similar...

You might find the section on dictionaries in my new tutor helpful:



Alan G
Author of the Learn to Program web tutor

More information about the Tutor mailing list