Creating 2D dictionary in Python

Something like this should work.

dictionary = dict()
dictionary[1] = dict()
dictionary[1][1] = 3
print(dictionary[1][1])

You can extend it to higher dimensions as well.


Something like this would work:

set1 = {
     'name': 'Michael',
     'place': 'London',
     ...
     }
# same for set2

d = dict()
d['set1'] = set1
d['set2'] = set2

Then you can do:

d['set1']['name']

etc. It is better to think about it as a nested structure (instead of a 2D matrix):

{
 'set1': {
         'name': 'Michael',
         'place': 'London',
         ...
         }
 'set2': {
         'name': 'Michael',
         'place': 'London',
         ...
         }
}

Take a look here for an easy way to visualize nested dictionaries.


It would have the following syntax

dict_names = {
    'd1': {
        'name': 'bob',
        'place': 'lawn',
        'animal': 'man'
    },
    'd2': {
        'name': 'spot',
        'place': 'bed',
        'animal': 'dog'
    }
}

You can then look things up like

>>> dict_names['d1']['name']
'bob'

To assign a new inner dict

dict_names['d1'] = {'name': 'bob', 'place': 'lawn', 'animal': 'man'}

To assign a specific value to an inner dict

dict_names['d1']['name'] = 'fred'