Reputation: 304
I have four things to push to a dict user_post_dict with a method push_to_dict
user_post_dict= {}
def push_to_dict(user_email, post_id, question_text, question_answer):
if user_email in user_post_dict:
if post_id in user_post_dict[user_email]:
user_post_dict[user_email][post_id][question_text] = question_answer
else:
user_post_dict[user_email][post_id] = {}
user_post_dict[user_email][post_id][question_text] = question_answer
else:
user_post_dict[user_email] = {}
user_post_dict[user_email][post_id] = {}
user_post_dict[user_email][post_id][question_text] = question_answer
push_to_dict('[email protected]',1,'what is this?', 'this is something')
push_to_dict('[email protected]',2,'what is that?', 'that is something')
push_to_dict('[email protected]',1,'what is this?', 'this is something')
push_to_dict('[email protected]',2,'what is that?', 'that is something')
Is there any better way to optimize the code or shortening the code.
Upvotes: 1
Views: 1022
Reputation: 779
You can use defaultdict
from collections import defaultdict
user_post_dict= defaultdict(lambda : defaultdict(defaultdict))
def push_to_dict(user_email, post_id, question_text, question_answer):
user_post_dict[user_email][post_id][question_text] = question_answer
Upvotes: 5
Reputation: 182
user_post_dict= {}
def push_to_dict(user_email, post_id, question_text, question_answer):
global user_post_dict
if user_email not in user_post_dict:
user_post_dict.update({user_email:{post_id:{}}})
if post_id not in user_post_dict[user_email]:
user_post_dict[user_email].update({post_id:{question_text: question_answer}})
else:
user_post_dict[user_email][post_id][question_text] = question_answer
push_to_dict('[email protected]',1,'what is this?', 'this is something')
push_to_dict('[email protected]',2,'what is that?', 'that is something')
push_to_dict('[email protected]',1,'what is this?', 'this is something')
push_to_dict('[email protected]',5,'what is that?', 'that is something')
Upvotes: 0
Reputation: 10909
Yes, there are multiple possibilties. The best "in spirit" answer is defaultdict as shown by the other posts. But honestly, the data structure sucks to work with and is not easiliy testable. For one, you should pass the argument in, not use a global variable. Otherwise, testing and reusing will be problematic. Further, a class structure could be better to encapsulate the data and provide easier access.
Upvotes: 1
Reputation: 36043
user_post_dict = {}
def push_to_dict(user_email, post_id, question_text, question_answer):
user_post_dict.setdefault(user_email, {}).setdefault(post_id, {})[question_text] = question_answer
push_to_dict('[email protected]', 1, 'what is this?', 'this is something')
push_to_dict('[email protected]', 2, 'what is that?', 'that is something')
push_to_dict('[email protected]', 1, 'what is this?', 'this is something')
push_to_dict('[email protected]', 2, 'what is that?', 'that is something')
assert user_post_dict == {'[email protected]': {1: {'what is this?': 'this is something'},
2: {'what is that?': 'that is something'}},
'[email protected]': {1: {'what is this?': 'this is something'},
2: {'what is that?': 'that is something'}}}
Upvotes: 0