Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update and create a multi-dimensional dictionary in Python

I am parsing JSON that stores various code snippets and I am first building a dictionary of languages used by these snippets:

snippets = {'python': {}, 'text': {}, 'php': {}, 'js': {}}

Then when looping through the JSON I'm wanting add the information about the snippet into its own dictionary to the dictionary listed above. For example, if I had a JS snippet - the end result would be:

snippets = {'js': 
                 {"title":"Script 1","code":"code here", "id":"123456"}
                 {"title":"Script 2","code":"code here", "id":"123457"}
}

Not to muddy the waters - but in PHP working on a multi-dimensional array I would just do the following (I am lookng for something similiar):

snippets['js'][] = array here

I know I saw one or two people talking about how to create a multidimensional dictionary - but can't seem to track down adding a dictionary to a dictionary within python. Thanks for the help.

like image 488
gregwhitworth Avatar asked Feb 14 '13 03:02

gregwhitworth


People also ask

Can you create multiple dictionary in Python?

In Python, a Nested dictionary can be created by placing the comma-separated dictionaries enclosed within braces.

What does update () do in Python?

The update() method inserts the specified items to the dictionary. The specified items can be a dictionary, or an iterable object with key value pairs.

Can you update dictionaries in Python?

Python Dictionary update() method updates the dictionary with the elements from another dictionary object or from an iterable of key/value pairs. Parameters: This method takes either a dictionary or an iterable object of key/value pairs (generally tuples) as parameters.


2 Answers

This is called autovivification:

You can do it with defaultdict

def tree():
    return collections.defaultdict(tree)

d = tree()
d['js']['title'] = 'Script1'

If the idea is to have lists, you can do:

d = collections.defaultdict(list)
d['js'].append({'foo': 'bar'})
d['js'].append({'other': 'thing'})

The idea for defaultdict it to create automatically the element when the key is accessed. BTW, for this simple case, you can simply do:

d = {}
d['js'] = [{'foo': 'bar'}, {'other': 'thing'}]
like image 179
JBernardo Avatar answered Oct 12 '22 23:10

JBernardo


From

snippets = {'js': 
                 {"title":"Script 1","code":"code here", "id":"123456"}
                 {"title":"Script 2","code":"code here", "id":"123457"}
}

It looks to me like you want to have a list of dictionaries. Here is some python code that should hopefully result in what you want

snippets = {'python': [], 'text': [], 'php': [], 'js': []}
snippets['js'].append({"title":"Script 1","code":"code here", "id":"123456"})
snippets['js'].append({"title":"Script 1","code":"code here", "id":"123457"})
print(snippets['js']) #[{'code': 'code here', 'id': '123456', 'title': 'Script 1'}, {'code': 'code here', 'id': '123457', 'title': 'Script 1'}]

Does that make it clear?

like image 21
placeybordeaux Avatar answered Oct 13 '22 00:10

placeybordeaux