I want to create a dict which can be accessed as:
d[id_1][id_2][id_3] = amount
As of now I have a huge ugly function:
def parse_dict(id1,id2,id3,principal, data_dict):
if data_dict.has_key(id1):
values = data_dict[id1]
if values.has_key[id2]
..
else:
inner_inner_dict = {}
# and so on
What is the pythonic way to do this?
note, that i input the principal.. but what i want is the amount..
So if all the three keys are there.. add principal to the previous amount!
Thanks
You may want to consider using
defaultdict:For example:
will create a
defaultdictofdefaultdicts ofdicts (I know..but it is right), to access it, you can simply do:without having to resort to using
if...elseto initialize.