Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating 2D dictionary in Python

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?

like image 461
user2921139 Avatar asked Sep 18 '14 23:09

user2921139


People also ask

Can you make a 2d dictionary in Python?

In Python, we can add dictionaries within a dictionary to create a two-dimensional dictionary. We can also print the two-dimensional dictionary using the json. dumps() method, which turns the input into a JSON string. The following code example demonstrates how we can create a two-dimensional dictionary in Python.

Is dictionary 2d array in Python?

In Python, you absolutely can, and you don't need to nest dictionaries either; just use a 2-tuple as the key (where the 2-tuple are the x,y index). I have a number of applications that do exactly that using a 2-tuple as a key into a dictionary to represent a grid in some form: my_map ={}

How do you create a dictionary in Python?

In Python, a dictionary can be created by placing a sequence of elements within curly {} braces, separated by 'comma'. Dictionary holds pairs of values, one being the Key and the other corresponding pair element being its Key:value.

How do you create a double dictionary in Python?

You can create a nested dictionary in Python by placing comma-separated dictionaries within curly braces {}. A Python nested dictionary allows you to store and access data using the key-value mapping structure within an existing dictionary.


3 Answers

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'
like image 85
Cory Kramer Avatar answered Oct 20 '22 04:10

Cory Kramer


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.

like image 40
elyase Avatar answered Oct 20 '22 03:10

elyase


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.

like image 3
shamiul97 Avatar answered Oct 20 '22 03:10

shamiul97