19

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?

user2921139
  • 1,471
  • 4
  • 16
  • 25
  • It would help to give a more specific and complete example. What's in `setx`, and what do you want `dict_names[setx]['name']` to return? – abarnert Sep 18 '14 at 23:56

3 Answers3

29

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'
Cory Kramer
  • 107,498
  • 14
  • 145
  • 201
  • 1
    I have {'name':'bob', 'place':'lawn', 'animal':'man'} part created but how do I assign it as key to d1? – user2921139 Sep 19 '14 at 00:05
  • 2
    `dict_names['d1'] = {'name':'bob', 'place':'lawn', 'animal':'man'}` – Cory Kramer Sep 19 '14 at 00:09
  • 1
    so this is like a regular 1D dictionary. – user2921139 Sep 19 '14 at 00:30
  • Sorry - i followed the entire thing. Just took me a few minutes, oddly even though i understand the dictionary concept clearly. The big structures kind of scare me I think! – user2921139 Sep 19 '14 at 00:47
  • How about the case when I need to get the top-level both by the first and second key - for instance, pull all the objects that have a "name" tag in them? – chiffa Mar 25 '17 at 14:57
  • Good answer! Is there a way we could do the above in a for loop; i.e. say there're k number of dictionaries with the same keys but different features. I'd like to combine them into a 2d dictionary in a for loop. I've tried initializing an empty dictionary and append. But that didn't work. Any help would be appreciated! – Noprogexprnce mathmtcn Mar 14 '18 at 19:03
3

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.

Community
  • 1
  • 1
elyase
  • 37,104
  • 11
  • 102
  • 114
3

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.

shamiul97
  • 177
  • 1
  • 2
  • 12