Creating 2D dictionary in Python

user2921139 picture user2921139 · Sep 19, 2014 · Viewed 44.8k times · Source

I have a list of details from an output for "set1" which are like "name", "place", "animal", "thing" and a "set2" with the same details.

I want to create a dictionary with dict_names[setx]['name']... etc On these lines.

Is that the best way to do it? If not how do I do it?

I am not sure how 2D works in dictionary.. Any pointers?

Answer

Cory Kramer picture Cory Kramer · Sep 19, 2014

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'