10

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.

2 Answers 2

18

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'}]
6
  • This is what I was going to suggest, but from his second code snippet it looks like he wants 'js' to return a list of dicts. Feb 14, 2013 at 4:00
  • @placeybordeaux I don't really like php, but I'm quite sure their "Array" object may behave as list or as a dict depending on the moon position or something like that...
    – JBernardo
    Feb 14, 2013 at 4:02
  • I have no love for PHP, but I was referring to snippets = {'js':{"id":"3"}{"id":"2"}}, it looks like he wants a list of dicts attached to js, text, python etc. I love defaultdicts and even more so recursivily defined defaultdicts, but it looks like they won't work for exactly what he wants. Feb 14, 2013 at 4:38
  • 1
    @placeybordeaux I thought he unintentionally pasted the line twice... Possibly a defaultdict(list) will solve the problem
    – JBernardo
    Feb 14, 2013 at 4:53
  • yes, I do want multiple dictionaries within so I will take a look at defaultdict(list), if you would like to provide an example of that as well that would be great. It looks as though the answer by @placeybordeaux would do what I'm looking for but I like learning different methods of approach when learning a new language. Thanks. Feb 14, 2013 at 6:10
8

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?

1
  • Cool, do you need it explained more at all? Please remember to accept the answer if it solves your problem. Feb 14, 2013 at 20:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.