I tried to create the perfect dictionary for my needs (dict that's containing a dict with values and a list). However it seems that I assigned the same reference over and over.
brands = ['val1', 'val2', 'val3']
infoBrands = dict.fromkeys(brands,
dict(dict.fromkeys(['nbOffers', 'nbBestOffers'], 0),
**dict.fromkeys(['higherPrice'], [])))
infoBrands['val1']['nbOffers'] += 1
print infoBrands
Here the results:
{'val3':
{'higherPrice': [],
'nbOffers': 1,
'nbBestOffers': 0},
'val2':
{'higherPrice': [],
'nbOffers': 1,
'nbBestOffers': 0},
'val1':
{'higherPrice': [],
'nbOffers': 1,
'nbBestOffers': 0}
}
As you can see, val1, val2 and val3 refer to the same dict. I'm not sure how I should handle it? Any tips?