I can't figure out how to update a value in a nested dictionary, the value keeps getting overwritten.
I have a list date_list = ['2018AUG15', '2017APR22', '2017MAR05', '2016FEB10', '2016FEB09']
I'm working on building a nested dict and so far I have
import collections
date_list = ['2018AUG15', '2017APR22', '2017MAR05', '2016FEB10', '2016FEB09']
month_ditc = collections.defaultdict(dict)
for x in date_list:
year = x[:4]
month = x[4:-2]
day = x[7:]
month_ditc[year][month]= day
print month_ditc
which yields
defaultdict(<type 'dict'>, {'2017': {'APR': '22', 'MAR': '05'}, '2016': {'FEB': '09'}, '2018': {'AUG': '15'}})
This is close to what I want. The year and month are updating as I loop through, but the day is not.
I have tried the following but still nothing -
try:
month_ditc[year][month] = day
except KeyError:
month_ditc[year] = {month:day}
I would like the result to be
defaultdict(<type 'dict'>, {'2017': {'APR': '22', 'MAR': '05'}, '2016': {'FEB': '09','10'}, '2018': {'AUG': '15'}})
ordered_string_list?daysto a list?