New submission from Admir Ljubijankić <ad...@virtua-it.si>: I'm writing a function that combines two dictionaries called "baseProducts" and "variantProducts". The logic behind is simple, I loop trough variantProducts, create a string that will be used as a key for that combined product. Add all values of baseProduct under that key, and add all fitting variants to this new nested dictionary under key "variants". And return the new dict once all variants are grouped correctly. The code looks something like this:
def combineProductsByColor(baseProducts, variantProducts): retDict = {} for key, variant in variantProducts.items(): productKey = variant['parent'] + '_' + variant['color'] if productKey not in retDict: retDict[productKey] = baseProducts[variant['parent']] retDict[productKey]['variants'] = {} retDict[productKey]['variants'][key] = variant return retDict Now with my test data, baseProducts only contains one item in it. While variantProducts contain 4 items. When the first two loops happen it creates a new key, and adds the first and second variant under that key correctly (this was tested using ms code debugger) but when it looks at the 3rd item, it creates a new key, and adds the variant to that key, but it also overrides all the variants in the previous key, even if the variable productKey is the new key. And when the 4th item gets processed it again gets added to both. So to make sure that I'm not getting some wrong key somehow. I also tried to implement it like this: def combineProductsByColor(baseProducts, variantProducts): retDict = {} keys = list(variantProducts.keys()) for k in keys: productKey = variantProducts[k]['parent'] + '_' + variantProducts[k]['color'] if productKey not in retDict: retDict[productKey] = baseProducts[variantProducts[k]['parent']] retDict[productKey]['variants'] = {} retDict[productKey]['variants'][k] = variantProducts[k] return retDict But the result was identical. Again following the whole procedure with ms code debugger. I've also tried making the productKey simpler, but the problem still remains. Now I do have an idea how to implement this in a different way, but I don't see and understand why the code I provided doesn't work. Because when the new key gets generated, its not the same as the old, but it still can change values under the old key. Here is what the data looks like before and after processing it. baseProducts: { '2321963000004': { 'code': 'P20-0002', 'name': 'Ženske hlače', 'regular_price': '22,99', 'vat': '22,00', 'attrib': { 'material': '97% bombaž, 3% elastan, 20% viskoza, 5% elastan' }, 'categories': ['01', '0101', '010104'] } } variantProducts: { '2321963991029': { 'parent': '2321963000004', 'color': '99', 'size': '102', 'title': None, 'name': None, 'regular_price': None, 'vat': None, 'attrib': None, 'categories': None }, '2321963991036': { 'parent': '2321963000004', 'color': '99', 'size': '103', 'title': None, 'name': None, 'regular_price': '25,99', 'vat': '22,00', 'attrib': None, 'categories': None }, '2321963981029': { 'parent': '2321963000004', 'color': '98', 'size': '102', 'title': None, 'name': None, 'regular_price': None, 'vat': None, 'attrib': None, 'categories': None }, '2321963981036': { 'parent': '2321963000004', 'color': '98', 'size': '103', 'title': None, 'name': None, 'regular_price': None, 'vat': None, 'attrib': None, 'categories': None } } And last combined data: { '2321963000004_99': { 'code': 'P20-0002', 'name': 'Ženske hlače', 'regular_price': '22,99', 'vat': '22,00', 'attrib': { 'material': '97% bombaž, 3% elastan, 20% viskoza, 5% elastan' }, 'categories': ['01', '0101', '010104'], 'variants': { '2321963981029': { 'parent': '2321963000004', 'color': '98', 'size': '102', 'title': None, 'name': None, 'regular_price': None, 'vat': None, 'attrib': None, 'categories': None }, '2321963981036': { 'parent': '2321963000004', 'color': '98', 'size': '103', 'title': None, 'name': None, 'regular_price': None, 'vat': None, 'attrib': None, 'categories': None } } }, '2321963000004_98': { 'code': 'P20-0002', 'name': 'Ženske hlače', 'regular_price': '22,99', 'vat': '22,00', 'attrib': { 'material': '97% bombaž, 3% elastan, 20% viskoza, 5% elastan' }, 'categories': ['01', '0101', '010104'], 'variants': { '2321963981029': { 'parent': '2321963000004', 'color': '98', 'size': '102', 'title': None, 'name': None, 'regular_price': None, 'vat': None, 'attrib': None, 'categories': None }, '2321963981036': { 'parent': '2321963000004', 'color': '98', 'size': '103', 'title': None, 'name': None, 'regular_price': None, 'vat': None, 'attrib': None, 'categories': None } } } } ---------- components: macOS messages: 374860 nosy: admir, ned.deily, ronaldoussoren priority: normal severity: normal status: open title: Overriding dictionary value under wrong key. type: behavior versions: Python 3.7 _______________________________________ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue41484> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com