Home > Net >  How to replace a key value when comparing between two lists
How to replace a key value when comparing between two lists

Time:06-29

Im currently facing and issue where I have a two list:

List1:

[
  {
    'gtin': '00195866290877',
    'amount': 'LOW',
    'modificationDate': '2022-06-23T09:59:27.000Z'
  },
  {
    'gtin': '00195866290884',
    'amount': 'LOW',
    'modificationDate': '2022-06-23T09:59:26.000Z'
  },
  {
    'gtin': '00195866290891',
    'amount': 'LOW',
    'modificationDate': '2022-06-24T08:56:27.000Z'
  },
  {
    'gtin': '00195866290907',
    'amount': 'LOW',
    'modificationDate': '2022-06-27T10:12:29.000Z'
  },
  {
    'gtin': '00195866290921',
    'amount': 'LOW',
    'modificationDate': '2022-06-23T09:59:25.000Z'
  },
  {
    'gtin': '00195866290945',
    'amount': 'LOW',
    'modificationDate': '2022-06-23T09:59:26.000Z'
  }
]

List2

[
  {
    'gtin': '00195866290877',
    'specifications': [
      {
        'localized': '1',
        
      }
    ],
  },
  {
    'gtin': '00195866290884',
    'specifications': [
      {
        'localized': '2',
        
      }
    ],
  },
  {
    'gtin': '00195866290891',
    'specifications': [
      {
        'localized': '3',
        
      }
    ],
  },
  {
    'gtin': '00195866290907',
    'specifications': [
      {
        'localized': '4',
        
      }
    ],
  },
  {
    'gtin': '00195866290914',
    'specifications': [
      {
        'localized': '5',
        
      }
    ],
  },
  {
    'gtin': '00195866290921',
    'specifications': [
      {
        'localized': '6',
        
      }
    ],
  },
  {
    'gtin': '00195866290938',
    'specifications': [
      {
        'localized': '7',
        
      }
    ],
  },
  {
    'gtin': '00195866290945',
    'specifications': [
      {
        'localized': '8',
        
      }
    ],
  },
  {
    'gtin': '00195866291010',
    'specifications': [
      {
        'localized': '9',
        
      }
    ],
  },
  {
    'gtin': '00195866291027',
    'specifications': [
      {
        'localized': '10',
        
      }
    ],
  },
  {
    'gtin': '00195866291034',
    'specifications': [
      {
        'localized': '11',
        
      }
    ],
  },
]

What I am trying to do is that I would like to go through the List1 and try to find if the gtin is in List2 and if it is, then I want to store the value of localized meaning that the output I want to have in this case would be:

[
  {
    'localized': '1',
    'amount': 'LOW',
    'modificationDate': '2022-06-23T09:59:27.000Z'
  },
  {
    'localized': '2',
    'amount': 'LOW',
    'modificationDate': '2022-06-23T09:59:26.000Z'
  },
  {
    'localized': '3',
    'amount': 'LOW',
    'modificationDate': '2022-06-24T08:56:27.000Z'
  },
  {
    'localized': '4',
    'amount': 'LOW',
    'modificationDate': '2022-06-27T10:12:29.000Z'
  },
  {
    'localized': '5',
    'amount': 'LOW',
    'modificationDate': '2022-06-23T09:59:25.000Z'
  },
  {
    'localized': '6',
    'amount': 'LOW',
    'modificationDate': '2022-06-23T09:59:26.000Z'
  }
]

However here falls my knowledge, how can I compare those two list and have an output to be as I wanted. To basically check if the gtin is in List2, if it is then take the value localized and replace it in List1 (Or create a new list works as well)?

CodePudding user response:

Try:

tmp = {d["gtin"]: d for d in list2}

out = [
    {
        "localized": tmp[d["gtin"]]["specifications"][0]["localized"],
        "amount": d["amount"],
        "modificationDate": d["modificationDate"],
    }
    for d in list1
    if d["gtin"] in tmp
]

print(out)

Prints:

[
    {
        "localized": "1",
        "amount": "LOW",
        "modificationDate": "2022-06-23T09:59:27.000Z",
    },
    {
        "localized": "2",
        "amount": "LOW",
        "modificationDate": "2022-06-23T09:59:26.000Z",
    },
    {
        "localized": "3",
        "amount": "LOW",
        "modificationDate": "2022-06-24T08:56:27.000Z",
    },
    {
        "localized": "4",
        "amount": "LOW",
        "modificationDate": "2022-06-27T10:12:29.000Z",
    },
    {
        "localized": "6",
        "amount": "LOW",
        "modificationDate": "2022-06-23T09:59:25.000Z",
    },
    {
        "localized": "8",
        "amount": "LOW",
        "modificationDate": "2022-06-23T09:59:26.000Z",
    },
]
  • Related