Trying to understand how to create nested dictionaries on the fly. Ideally my dictionary would look something like:
mydict = { 'Message 114861156': { 'email': ['user1@domain.com', 'user2@domain.com'] }, { 'status': 'Queued mail for delivery' }}
Here's what i have so far:
sampledata = "Message 114861156 to user1@domain.com user2@domain.com [InternalId=260927844] Queued mail for delivery'."
makedict(sampledata)
def makedict(results):
newdict = {}
for item in results:
msgid = re.search(r'Message \d+', item)
msgid = msgid.group()
newdict[msgid]['emails'] = re.findall(r'\w+@\w+\.\w+', item)
newdict[msgid]['status'] = re.findall(r'Queued mail for delivery', item)
has the following output:
Traceback (most recent call last):
File "wildfires.py", line 57, in <module>
striptheshit(q_result)
File "wildfires.py", line 47, in striptheshit
newdict[msgid]['emails'] = re.findall(r'\w+@\w+\.\w+', item)
KeyError: 'Message 114861156'
How do you make a nested dictionary like this on the fly?