Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def insert(dictionary, tree, value):
- '''it will create key if doesn't exist'''
- for key in tree[:-1]: # without last key
- if key not in dictionary:
- dictionary[key] = {}
- dictionary = dictionary[key]
- dictionary[tree[-1]] = value # last key
- def get(dictionary, tree):
- '''it will raise error if key doesn't exist'''
- for key in tree: # without last key
- dictionary = dictionary[key]
- return dictionary # last key
- #------------------------------------------------------
- usr = {}
- comment = 'hello world'
- tree = ['acc', 'timeline', 'last_post', 'comments']
- insert(usr, tree, comment)
- print(usr)
- print( get(usr, tree) )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement