I have two lists that are full of dictionaries.
l1 = l1 = [{'sku': '1122564', 'mfg': 'KANCA', 'mfgPartNumber': 'KC6247VMV2', 'import productNo': 'KANCA-KC6247VMV2', 'dept': 11, 'class': 30},
{'sku': '1122565', 'mfg': 'KANCA', 'mfgPartNumber': 'KC6247VMV2', 'import productNo': 'KANCA-KC6247VMV2', 'dept': 11, 'class': 30},
{'sku': '1122566', 'mfg': 'KANCA', 'mfgPartNumber': 'KC6247VMV2', 'import productNo': 'KANCA-KC6247VMV2', 'dept': 11, 'class': 30},
{'sku': '1122567', 'mfg': 'KANCA', 'mfgPartNumber': 'KC6247VMV2', 'import productNo': 'KANCA-KC6247VMV2', 'dept': 11, 'class': 30}]
l2 = [{'mfgPartNumber': 'KC6247VMV2', 'selected mgmt structure': 'A2021051106', 'mapped webStructure': '380'}]
for this particular case, l1 contains a total of 92 dictionaries and l2 contains 12 dictionaries, but I am showing you an example with less cases (for those who can't see how obvious that is).
I've tried this:
l3 = l1.update(l2)
and this:
def Merge(d1, d2)):
result = d1 | d2
return result
l3 = Merge(l1, l2)
But this didn't work. What I'm really looking for is this:
l3 = [{'sku': '1122564', 'mfg': 'KANCA', 'mfgPartNumber': 'KC6247VMV2', 'import productNo': 'KANCA-KC6247VMV2', 'dept': 11, 'class': 30, 'selected mgmt structure': 'A2021051106', 'mapped webStructure': '380'},
{'sku': '1122565', 'mfg': 'KANCA', 'mfgPartNumber': 'KC6247VMV2', 'import productNo': 'KANCA-KC6247VMV2', 'dept': 11, 'class': 30, 'selected mgmt structure': 'A2021051106', 'mapped webStructure': '380'},
{'sku': '1122566', 'mfg': 'KANCA', 'mfgPartNumber': 'KC6247VMV2', 'import productNo': 'KANCA-KC6247VMV2', 'dept': 11, 'class': 30, 'selected mgmt structure': 'A2021051106', 'mapped webStructure': '380'},
{'sku': '1122567', 'mfg': 'KANCA', 'mfgPartNumber': 'KC6247VMV2', 'import productNo': 'KANCA-KC6247VMV2', 'dept': 11, 'class': 30, 'selected mgmt structure': 'A2021051106', 'mapped webStructure': '380'}]
Can anyone help out?
Much Appreciated.
CodePudding user response:
It looks like you are looking for something like this:
l1 = [{'sku': '1122564', 'mfg': 'KANCA', 'mfgPartNumber': 'KC6247VMV2', 'import productNo': 'KANCA-KC6247VMV2', 'dept': 11, 'class': 30},
{'sku': '1122565', 'mfg': 'KANCA', 'mfgPartNumber': 'KC6247VMV2', 'import productNo': 'KANCA-KC6247VMV2', 'dept': 11, 'class': 30},
{'sku': '1122566', 'mfg': 'KANCA', 'mfgPartNumber': 'KC6247VMV2', 'import productNo': 'KANCA-KC6247VMV2', 'dept': 11, 'class': 30},
{'sku': '1122567', 'mfg': 'KANCA', 'mfgPartNumber': 'KC6247VMV2', 'import productNo': 'KANCA-KC6247VMV2', 'dept': 11, 'class': 30}]
l2 = [{'mfgPartNumber': 'KC6247VMV2', 'selected mgmt structure': 'A2021051106', 'mapped webStructure': '380'}]
def merge(l1, l2):
l2_merged = {}
for d in l2:
l2_merged.update(d)
return [d | l2_merged for d in l1]
print(merge(l1, l2))
However, this depends on exactly how you want the dictionaries in l2 to be merged, which is not so clear from your question.
CodePudding user response:
Here you go:
l1 = [{'sku': '1122564', 'mfg': 'KANCA', 'mfgPartNumber': 'KC6247VMV2', 'import productNo': 'KANCA-KC6247VMV2', 'dept': 11, 'class': 30},
{'sku': '1122565', 'mfg': 'KANCA', 'mfgPartNumber': 'KC6247VMV2', 'import productNo': 'KANCA-KC6247VMV2', 'dept': 11, 'class': 30},
{'sku': '1122566', 'mfg': 'KANCA', 'mfgPartNumber': 'KC6247VMV2', 'import productNo': 'KANCA-KC6247VMV2', 'dept': 11, 'class': 30},
{'sku': '1122567', 'mfg': 'KANCA', 'mfgPartNumber': 'KC6247VMV2', 'import productNo': 'KANCA-KC6247VMV2', 'dept': 11, 'class': 30}]
l2 = [{'mfgPartNumber': 'KC6247VMV2', 'selected mgmt structure': 'A2021051106', 'mapped webStructure': '380'}]
l3 = [dct | l2[0] for dct in l1]
[print(x) for x in l3]
Output:
{'sku': '1122564', 'mfg': 'KANCA', 'mfgPartNumber': 'KC6247VMV2', 'import productNo': 'KANCA-KC6247VMV2', 'dept': 11, 'class': 30, 'selected mgmt structure': 'A2021051106', 'mapped webStructure': '380'}
{'sku': '1122565', 'mfg': 'KANCA', 'mfgPartNumber': 'KC6247VMV2', 'import productNo': 'KANCA-KC6247VMV2', 'dept': 11, 'class': 30, 'selected mgmt structure': 'A2021051106', 'mapped webStructure': '380'}
{'sku': '1122566', 'mfg': 'KANCA', 'mfgPartNumber': 'KC6247VMV2', 'import productNo': 'KANCA-KC6247VMV2', 'dept': 11, 'class': 30, 'selected mgmt structure': 'A2021051106', 'mapped webStructure': '380'}
{'sku': '1122567', 'mfg': 'KANCA', 'mfgPartNumber': 'KC6247VMV2', 'import productNo': 'KANCA-KC6247VMV2', 'dept': 11, 'class': 30, 'selected mgmt structure': 'A2021051106', 'mapped webStructure': '380'}