I'm going to create a tree using python dict without knowing the exact number of levels and nodes before doing it.
For example I'm gonna loop many items with 3 attributes: size, color, and weight. In each loop I do the following things
item_data = {}
for item in items:
size, color, weight = item.get_attr()
if size not in item_data:
item_data[size] = {}
if color not in item_data[size]:
item_data[size][color] = {}
if weight not in item_data[size][color]:
item_data[size][color][weight] = []
# Do something to the tree...
Finally I get a dict like this
item_data == {'L':{
'red':{45:[...],
50:[...]},
'green':{
40:[...]}},
'XL':{...}}
But this is not flexible. For example if I want to add another attribute like 'price'? I have to know it and add one more if in the codes above.
I'm thinking about doing it in the following way, but don't know how to do it in few lines
attrs = [item.get_attr()]
for attr in attrs:
# Create the tree here..
Thanks in advance!